滚动浏览div

时间:2016-09-08 10:25:05

标签: javascript jquery html css twitter-bootstrap

我试图让我的引导导航栏在向下滚动到特定div之后成为固定的顶部导航栏。

不确定最好的方法是什么,但是我的代码在滚动后没有将类添加回导航栏。

任何更简单的解决方案?

   $(document).ready(function(){

   // if on homepage
   if ( window.location.pathname == '/' ){


      $("#nav").removeClass("navbar-fixed-top");
      $("#nav").css("position", "absolute");

      $(window).scroll(function() {

       // get positions of nav and div element

       var thediv =$("#home-callout-top").offset();
       var thenav = $("#nav").offset();
       console.log("the div" + thediv.top + "nav" + thenav.top);

      if(thenav.top >= thediv.top){ 

            // If nav top position is greater (below) the div then make the navbar fixed top again

            $("#nav").addClass("navbar-fixed-top");
            $("#nav").css("position", "relative");
      }
 });
 }
});

0 个答案:

没有答案