你能帮我解决一件我不能处理的事吗?
我在我的网站上使用了滚动-Nav.js作为顶部菜单-内部、外部等architect/services.php。
现在,我使用以下代码设置偏移量
$(window).scroll(function() {
if ($(".navbar").offset().top > 130) {
    $(".navbar-fixed-top").addClass("top-nav-collapse");
} else {
    $(".navbar-fixed-top").removeClass("top-nav-collapse");
}
});
$(function() {
$('a.page-scroll').bind('click', function(event) {
    var $anchor = $(this);
    $('html, body').stop().animate({
        scrollTop: $($anchor.attr('href')).offset().top - 130
    }, 1500, 'easeInOutExpo');
    event.preventDefault();
});
});但是,当您单击外部时,active类并不完全在此位置工作。
我试着用方法形成另一个话题,但我的"js“知识很差。jquery scroll, change navigation active class as the page is scrolling, relative to sections
提前谢谢。
发布于 2015-03-20 21:49:49
现在,仅当节的顶部偏移量为0时才应用active类。您可以使用jquery将其更改为其他值,如130。添加以下代码:
$(window).scroll(function(){
    /* Get id of sections corresponding to top nav menu */
    var scroll_sections = []
    $('a.page-scroll').each(function(){
      scroll_sections.push($(this).attr('href'));
    })
    for (i in scroll_sections)
    {   
      /* Instead of 0, if the top position offset of section is 130 or less,
         we add active class for that section in nav menu */
      if ($(scroll_sections[i]).position().top <= $(window).scrollTop() + 130) 
      {
        $('nav li.active').removeClass('active');
        $('nav a').eq(i).parent().addClass('active');
      }
    }
});https://stackoverflow.com/questions/29175034
复制相似问题