简体   繁体   English

导航栏在滚动时固定。 用javascript

[英]Navbar fixed when scroll. With javascript

I don't code javascript so it is a problem for me to understand how to develop my navbar to look like this: http://www.bootply.com/kD5wiG5udv 我不编写JavaScript代码,因此对我来说,了解如何开发导航栏使其看起来像这样是一个问题: http : //www.bootply.com/kD5wiG5udv

I have obviously used common sense and swapped my selectors but it still isn't working. 我显然已经使用了常识并交换了选择器,但是它仍然无法正常工作。 Could someone please advise me on what steps to take next? 有人可以建议我下一步该怎么做吗?

Please use the above website to navigate through my code as I am not going to add the bootstrap files to this post. 请使用上述网站浏览我的代码,因为我不会在该帖子中添加引导文件。 They are too large. 它们太大了。

Benjamin 本杰明

<nav class="navbar navbar-default" role="navigation" id="nav">
  <div class="container-fluid">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle Navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
      <a class="navbar-brand" href="#">
        <img alt="" src="">
      </a>
    </div>
    <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">Projects</li>
            <li><a href="#">Services</li>
            <li><a href="#">About</li>
            <li><a href="#">Contact</li>
        </ul>
    </div>
  </div>
</nav>

CSS: CSS:

.navbar-default

{

  background-color: #fbfbfb;
}

.navbar-nav {
    float:none;
    margin:0 auto;
    display: block;
    text-align: center;
}

.navbar-nav > li {
    display: inline-block;
    float:none;
}

.scroll-top {
   position:fixed;
   bottom:0;
   right:6%;
   z-index:100;
   background: #f2f3f2;
   font-size:24px;
   border-top-left-radius:3px;
   border-top-right-radius:3px;
}
.scroll-top a:link,.scroll-top a:visited {
  color:#222;
} 

.navbar-nav {

  margin-left: 50px;
  margin-top: -5px;
}

Javascript: 使用Javascript:

/* affix the navbar after scroll below header */
$('#nav').affix({
      offset: {
        top: $('header').height()-$('#nav').height()
      }
}); 

/* highlight the top nav as scrolling occurs */
$('body').scrollspy({ target: '#nav' })

/* smooth scrolling for scroll to top */
$('.scroll-top').click(function(){
  $('body,html').animate({scrollTop:0},1000);
})

/* smooth scrolling for nav sections */
$('#nav .navbar-nav li>a').click(function(){
  var link = $(this).attr('href');
  var posi = $(link).offset().top;
  $('body,html').animate({scrollTop:posi},700);
});

In my opinion it would be great to do as much as possible in CSS if your website does not to be compatible with old versions of the browsers. 我认为,如果您的网站与旧版本的浏览器不兼容,那么最好在CSS中做更多的事情。 I'm not sure if the Bootstrap has it's own way to implement something similar, but here you can get the idea on how you'd do this yourself. 我不确定Bootstrap是否有自己的方法来实现类似的操作,但是在这里您可以了解如何自己执行此操作。 The main role of jQuery here would be just to add a class on scroll event, which would trigger an animation of opacity. jQuery的主要作用是在滚动事件上添加一个类,这将触发不透明动画。 This should be much cleaner solution for the problem as it would be smooth and fast. 这应该是解决问题的更干净的方法,因为它既平滑又快速。 Here is the working fiddle: https://jsfiddle.net/q5jo781c/1/ 这是工作的小提琴: https : //jsfiddle.net/q5jo781c/1/

This part toggles class on scroll: 这部分在滚动上切换类:

$(document).ready(function(){
    $(document).scroll(function(){
        var st = $(this).scrollTop();
        if(st > 200) {
            $("navbar").addClass('fixed');
        } else {
            $("navbar").removeClass('fixed');
        }
    });
});

And the CSS: 和CSS:

navbar {
  width: 100%;
  display: block;
  height: 50px;
  background-color: black;
}

navbar.fixed {
  position: fixed;
  top: 0;
  left: 0;
  background-color: white;
  animation-name: example;
  animation-duration: 1s;
}

@keyframes example {
  from {opacity: 0;}
  to {opacity: 1;}
}

You can find more of the CSS in the fiddle. 您可以在小提琴中找到更多的CSS。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM