简体   繁体   中英

How to highlighting active menu item on scroll and click?

I want to add a class active to the current menu item on scroll and click. It's a single (long) page with different sections. When click on an item, the active state should switch to the current one.

I came up with the following code, but doesn't now how I can integrate the above:

// Click event
    $('#primary-navwrapper li a[href^="#"]').click(function(event) {

        // Prevent from default action to intitiate
        event.preventDefault();

        // The id of the section we want to go to
        var anchorId = $(this).attr('href');

        // Our scroll target : the top position of the section that has the id referenced by our href
        var target = $(anchorId).offset().top - offset;
        console.log(target);

        $('html, body').stop().animate({ scrollTop: target }, 500, function () {
            window.location.hash = anchorId;
        });

        return false;
    });

To add active class on click is simple using jQuery. You just need this code in the click handler

//remove active from all anchor and add it to the clicked anchor
        $('#primary-navwrapper li a[href^="#"]').removeClass("active")
        $(this).addClass('active');

And for the scroll part you need to monitor the position of the scroll bar and compare it with every page offset like so

//check the pages when scroll event occurs
$(window).scroll(function(){
    // Get the current vertical position of the scroll bar
    position = $(this).scrollTop();
    $('#primary-navwrapper li a[href^="#"]').each(function(){
          var anchorId = $(this).attr('href'); 
           var target = $(anchorId).offset().top - offset;
           // check if the document has crossed the page
        console.log(position,target);
        if(position>=target){
             //remove active from all anchor and add it to the clicked anchor
            $('#primary-navwrapper li a[href^="#"]').removeClass("active")
            $(this).addClass('active'); 
        }
    })

Here is a demo http://jsfiddle.net/k5afwfva/

<nav>
    <a href="#" class="item">item</a>
    <a href="#" class="item">item</a>
    <a href="#" class="item">item</a>
    <a href="#" class="item">item</a>
</nav>


var el = $(".item"),
    yPos = 0;


el.click(function(event){
    event.preventDefault();

    $(this).addClass("active").siblings().removeClass("active");
});
$(window).scroll(function(){

    yPos = $(this).scrollTop();

    //i'm almost sure that you will need to calculate offset of your section to know when to switch classes

    if(yPos > 100){
        el.removeClass("active").eq(1).addClass("active");
    }
    if(yPos > 200){
        el.removeClass("active").eq(2).addClass("active");
    }
    //etc....
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM