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

时间:2015-07-17 18:39:19

标签: javascript smooth-scrolling

我有一个单页滚动网站,想要在滚动和点击时更改链接的活动类。我从这个网站上找到了一些很棒的片段,它们完成了一半的工作(对于那些感兴趣的人也可以顺利滚动):

$(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");
        }
    });
}

问题在于我有多个菜单在不同的屏幕尺寸下可见。我正在努力想要同时更改两个菜单项上的活动类。

任何建议都将不胜感激

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题,但我欢迎更清洁的解决方案。我只是复制代码:`

&#13;
&#13;
$(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");
        }
    });
}
&#13;
&#13;
&#13;

虽然辅助菜单的ID不同:

&#13;
&#13;
$(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");
        }
	});
}
&#13;
&#13;
&#13;

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

相关问题