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

前端 未结 3 660
青春惊慌失措
青春惊慌失措 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:55

    If you wish a more generic function:

    SEE DEMO

    $(window).scroll(function() {
        var windscroll = $(window).scrollTop();
        if (windscroll >= 100) {
            $('nav').addClass('fixed');
            $('.wrapper section').each(function(i) {
                if ($(this).position().top <= windscroll - 100) {
                    $('nav a.active').removeClass('active');
                    $('nav a').eq(i).addClass('active');
                }
            });
    
        } else {
    
            $('nav').removeClass('fixed');
            $('nav a.active').removeClass('active');
            $('nav a:first').addClass('active');
        }
    
    }).scroll();​
    

提交回复
热议问题