jquery scroll, change navigation active class as the page is scrolling, relative to sections

前端 未结 3 658
青春惊慌失措
青春惊慌失措 2020-11-29 21:33

http://jsfiddle.net/motocomdigital/gUWdJ/


I\'m after a jquery scroll technique please that I would like to adapt to my project.

Please see my

3条回答
  •  长情又很酷
    2020-11-29 21:58

    You can do this way: http://jsfiddle.net/gUWdJ/1/

    $(window).scroll(function() {
    
        if ($(this).scrollTop() < $('section[data-anchor="top"]').offset().top) {
           $('nav a').removeClass('active');
        }
    
        if ($(this).scrollTop() >= $('section[data-anchor="top"]').offset().top) {
          $('nav a').removeClass('active');
          $('nav a:eq(0)').addClass('active');
        }
        if ($(this).scrollTop() >= $('section[data-anchor="news"]').offset().top) {
          $('nav a').removeClass('active');
          $('nav a:eq(1)').addClass('active');
        }
        if ($(this).scrollTop() >= $('section[data-anchor="products"]').offset().top) {
          $('nav a').removeClass('active');
          $('nav a:eq(2)').addClass('active');
        }
        if ($(this).scrollTop() >= $('section[data-anchor="contact"]').offset().top) {
          $('nav a').removeClass('active');
          $('nav a:eq(3)').addClass('active');
        }
    
    });
    

提交回复
热议问题