用多个菜单改变滚动上的活动菜单项

Change Active Menu Item on Scroll with Multiple Menus

本文关键字:活动 菜单项 滚动 改变 菜单      更新时间:2023-09-26

我有一个单页滚动网站,并想改变我的链接的活动类滚动和点击。我从这个网站上找到了一些很棒的片段,它们完成了一半的工作(对于那些感兴趣的人来说,这也可以实现平滑滚动):

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

出现的问题是,我有多个菜单,在不同的屏幕尺寸可见。我正在努力寻找一种方法来同时改变两个菜单项上的活动类。

如有任何建议,不胜感激

我自己解决了这个问题,但我希望有一个更干净的解决方案。我只是复制了下面的代码:'

$(document).ready(function () {
    $(document).on("scroll", onScroll);
    
    //smoothscroll
    $('#nav-minor a[href^="#"]').on('click', function (e) {
        e.preventDefault();
        $(document).off("scroll");
        
        $('#nav-minor a').each(function () {
            $(this).removeClass('active');
        })
        $(this).addClass('active');
      
        var target = this.hash,
            menu = target;
        $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top+2
        }, 500, 'swing', function () {
            window.location.hash = target;
            $(document).on("scroll", onScroll);
        });
    });
});
function onScroll(event){
    var scrollPo = $(document).scrollTop();
    $('#nav-minor a').each(function () {    
        var currLink = $(this);
        var refElement = $(currLink.attr("href"));
        if (refElement.position().top <= scrollPo && refElement.position().top + refElement.height() > scrollPo) {
            $('#nav-minor a').removeClass("active");
            currLink.addClass("active");
        }
        else{
            currLink.removeClass("active");
        }
    });
}

虽然二级菜单的ID不同:

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

如果有人有更好的答案将不胜感激。现在纯粹出于学术原因:-)'