jquery 自动隐藏 Bootstrap 中导航栏上方的警报

dz6r00yl  于 2023-01-25  发布在  jQuery
关注(0)|答案(2)|浏览(137)

大家好,我想给你们看隐藏和钉在导航栏上,我得到了它显示,但他的动画它不工作,而隐藏..有人可以帮我修复它?
fiddle上查看
//参数设置

.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;
}

//显示#通知栏

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);

//隐藏#通知栏

setTimeout(function () {

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

    }, 6000);

//网页

<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>
xkftehaa

xkftehaa1#

这是一个基本的解决方案。
原始非固定.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>
zwghvu4y

zwghvu4y2#

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>

相关问题