Single page navigation menu - active indicator

后端 未结 1 1028
遇见更好的自我
遇见更好的自我 2021-02-10 15:22

I\'m trying to update the navigation for a single page website using jQuery waypoints.

I want to update the navigation based on the current section in view. It works fi

1条回答
  •  长情又很酷
    2021-02-10 15:55

    Try this: your modified example

    $(document).ready(function(){
            $('section').waypoint(function(direction) {
              var me = $(this); //added
              thisId = $(this).attr('id');
              $('ul li').each(function(){
                var secondaryID = $(this).attr('class');
                if  ( secondaryID == thisId )
                    {
                        $('ul li').removeClass('active');
    
                        //added
                        if(direction==='up'){
                            me = $(this).prev();
                        }
    
                        //added
                        if(!me.length){
                            me = $(this);
                        }
    
                        $(this).addClass('active');
                    }
                });
        },{offset: '0'});   
    }); 
    

    UPD ok, how about this example:

    The idea is:

    1) When we hit section N top border, while scrolling down, means active section become section N + 1.

    2) When we hit section N top border, while scrolling top, means section N become active.

    PS. sorry for my english

    Javascript

    $(document).ready(function(){
            $('section').waypoint(function(direction) {
                var activeSection = $(this);
                if(direction === 'down'){
                    activeSection = $(this).next();
                }
                //activeSection = $(this);
                var sectionId   = activeSection.attr('id');
                $('ul li').removeClass('active');
                $('ul li.' + sectionId).addClass('active');
                console.log(activeSection);
            });
    }); 
    
    $('a[href*=#]:not([href=#])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
            || location.hostname == this.hostname) {
    
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
               if (target.length) {
                 $('html,body').animate({
                     scrollTop: target.offset().top - (target.height() / 5)
                }, 500);
                return false;
            }
        }
    });
    

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