Change navigation active class on window scroll

前端 未结 5 1384
心在旅途
心在旅途 2020-12-28 11:31

i want to create a scroll effect like here http://www.strikingly.com/s/pages/default-ion?demo=ion#1 All i need for this moment is to change navigation li class active when

5条回答
  •  时光说笑
    2020-12-28 12:10

    hiccup in IE9 so I corrected

    $(document).ready(function () {
        $(document).on("scroll", onScroll);
    
        //smoothscroll
        $('a[href^="#"]').click(function(){
        var target = $(this).attr('href');
        $('html, body').animate({scrollTop: $(target).offset().top-48}, 500);
        return false;
    
            $("#menu a").click(function () {
                $("#menu a").removeClass('active');
            })
            $(this).addClass('active');
    
            var target = this.hash,
                menu = target;
            $target = $(target);
            $('html, body').stop().animate({
                'scrollTop': $(target).offset().top-50}, 500, 'swing', function () {
                window.location.hash = target;
                $(document).on("scroll", onScroll);
            });
        });
    });
    
    function onScroll(event){
        var scrollPos = $(document).scrollTop();
        j$('#menu a').each(function () {
            var currLink = $(this);
            var refElement = $(currLink.attr("href"));
            if (refElement.position().top-70 <= scrollPos && refElement.position().top-50 + refElement.height() > scrollPos) {
               $('#menu ul li a').removeClass("active");
                currLink.addClass("active");
            }
            else{
                currLink.removeClass("active");
            }
        });
    }
    

提交回复
热议问题