Highlight active link when using scrollto based on current view

后端 未结 1 1316
难免孤独
难免孤独 2020-12-15 00:29

I have a website which is one page and the user navigates to each section using links which uses the scrollto jquery plugin.

My problem is: I want to show the act

相关标签:
1条回答
  • 2020-12-15 01:14

    This should work for you to add the manual scrolling override:

    $(function(){
        var sections = {},
            _height  = $(window).height(),
            i        = 0;
    
        // Grab positions of our sections 
        $('.section').each(function(){
            sections[this.name] = $(this).offset().top;
        });
    
        $(document).scroll(function(){
            var pos = $(this).scrollTop();
    
            // Look in the sections object and see if any section is viewable on the screen. 
            // If two are viewable, the lower one will be the active one. 
            for(i in sections){
                if(sections[i] > pos && sections[i] < pos + _height){
                    $('a').removeClass('active');
                    $('#nav_' + i).addClass('active');
                }  
            }
        });
    });
    

    Demo: http://jsfiddle.net/x3V6Y/

    0 讨论(0)
提交回复
热议问题