在rails应用程序中滚动时,bootstrap 4 navbar淡入\ out

时间:2017-12-04 02:26:14

标签: javascript ruby-on-rails bootstrap-4

我试图弄清楚如何在引导程序4中使用顶部导航栏,然后当用户在页面上向下滚动时,导航栏淡入我现在可以获得导航栏显示使用下面的脚本,然而,它不会淡入它只是立即显示非常快速和笨拙。

<script>
$(document).scroll(function(e){
    var scrollTop = $(document).scrollTop();
    if(scrollTop > 200){

        $('.navbar').removeClass('static-top').addClass('fixed-top');

    } else {
        $('.navbar').removeClass('fixed-top').addClass('static-top');
    }
});
</script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <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 dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav><!--./nav-->

我正在寻找像http://bootstraplovers.com/assan-v3.6/classic-template/html/index.html

这样的导航栏效果

1 个答案:

答案 0 :(得分:0)

$('.navbar').removeClass('static-top').addClass('fixed-top');

在调用.removeClass()之前,您需要调用.toggle()或其他类似的jquery效果。请参阅documentation

.toggle()会隐藏导航栏,所以也许你想在相关的stackoverflow部分(jquery)中发布你的问题,考虑你想如何构建这个动画并搜索jquery文档......