在引导程序中导航栏上方自动隐藏警报

时间:2018-08-25 15:58:33

标签: jquery twitter-bootstrap jquery-ui navbar

大家好,我想向您展示隐藏并钉在导航栏上,我看到了它的显示,但是他的动画在隐藏时不起作用..有人可以帮我修复它吗?

fiddle上查看

// CSS

.margin-top-0 { margin-top: 0px !important; }
.margin-top-20 { margin-top: 20px !important; }
.margin-top-40 { margin-top: 40px !important; }
.alert-server {
  border-radius: 0;
  position:fixed;
  top:0;
  width:100%;
  padding:10px 0;
  text-align:center;
  display:none;
}

//显示#notificationBar

setTimeout(function () {

    // working
    $("#notificationBar").css({"display":"block"});
    $('#notificationBar').html('Hello User! Welcome!').addClass('margin-top-0', 2000);
    $('body, .navbar').addClass('margin-top-40', 2000);

}, 2000);

//隐藏#notificationBar

setTimeout(function () {

        // not working
        $('#notificationBar').removeClass('margin-top-0', 2000).html().css({"display":"none"});
        $('body, .navbar').removeClass('margin-top-40', 2000);

    }, 6000);

// HTML

<body>              
    <div id="notificationBar" class="alert alert-success alert-server" role="alert" style="display: none; overflow: hidden; z-index: 9999; margin-top: -40px;">
      <button type="button" class="close" data-dismiss="alert">×</button>
    </div>      

    <div class="navbar-spacer" style="min-height:60px;"></div>                  

     <!-- Fixed navbar -->
     <nav class="website-nav navbar navbar-default navbar-fixed-top">                    
        <div class="navbar-inner">
            <div class="nav-center">
            </div>
        </div>
     </nav>

1 个答案:

答案 0 :(得分:1)

这是一个基本的解决方案。

原始的非固定.navbar版本http://jsfiddle.net/joshmoto/vr4xto0a/1/

使用固定的导航栏时,我不得不采取一种略有不同的方法,即在主体上添加类,以显示警报以及为导航栏设置动画并增加主体填充。

查看新的.navbar.fixed-top版本http://jsfiddle.net/joshmoto/1a620ho8/

showAlert = function() {
  $('body').addClass('alert-show');
}

hideAlert = function() {
  $('body').removeClass('alert-show');
}

// auto show
setTimeout(function() {
  showAlert();
}, 1000);

// auto hide
setTimeout(function() {
  hideAlert();
}, 5000);
@import "http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css";

BODY {
  min-height: 75rem;
  padding-top: 4.5rem;
  transition: padding .5s ease-in-out;
}

.navbar.fixed-top {
  transition: top .5s ease-in-out;
}

.alert {
  position: fixed;
  width: 100%;
  top: -50px;
  transition: top .5s ease-in-out;
}

BODY.alert-show {
  padding-top: calc(4.5rem + 50px);
}

BODY.alert-show .alert {
  top: 0;
}

BODY.alert-show .navbar.fixed-top {
  top: 50px;
}
<div class="alert alert-success mb-0 text-center" role="alert">
  I'm going to disapear in 5000ms
</div>

<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  <a class="navbar-brand" href="#">Fixed navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
  <div class="collapse navbar-collapse" id="navbarCollapse">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline mt-2 mt-md-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

<main role="main" class="container">
  <div class="jumbotron">
    <h1>Navbar example</h1>
    <p class="lead">This example is a quick exercise to illustrate how fixed to top navbar works. As you scroll, it will remain fixed to the top of your browser's viewport.</p>
    <a class="btn btn-lg btn-primary" href="../../components/navbar/" role="button">View navbar docs &raquo;</a>
  </div>
</main>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>