Javascript jQuery平滑滚动链接颜色更改不起作用

Javascript jQuery平滑滚动链接颜色更改不起作用,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我使用jQuery设计了一个平滑的滚动条。我面临的一个问题是,当我点击navbar链接时,会出现闪烁 假设我点击Platform链接,那么Why v9链接上会闪烁。再次,当我点击其他链接时,上一个链接会闪烁。我无法调试它 有人能帮我吗?这会有很大的帮助 $(文档).ready(函数(){ $('.nav项')。单击(函数(){ $(“.nav item”).removeClass('active'); $(this.addClass('active'); $('html,body')。设置动画

我使用jQuery设计了一个平滑的滚动条。我面临的一个问题是,当我点击
navbar
链接时,会出现闪烁

假设我点击
Platform
链接,那么
Why v9
链接上会闪烁。再次,当我点击其他链接时,上一个链接会闪烁。我无法调试它

有人能帮我吗?这会有很大的帮助

$(文档).ready(函数(){
$('.nav项')。单击(函数(){
$(“.nav item”).removeClass('active');
$(this.addClass('active');
$('html,body')。设置动画({
滚动顶部:$($(this.children().attr('href')).offset().top
}, 800);
返回false;
});
//主动链接
$(窗口)。滚动(函数(){
var scrollbarLocation=$(this.scrollTop();
$('.nav link')。每个(函数(){
var sectionOffset=$($(this.attr('href')).offset().top;

如果(sectionOffset您可以添加一个全局变量,以确定是否通过单击或滚动进行滚动:

 $(document).ready(function () {
var clickScroll = false; //ADDED

    $('.nav-item').click(function () {
    clickScroll = true; //ADDED
        $(".nav-item").removeClass('active');
        $(this).addClass('active');
        $('html, body').animate({
            scrollTop: $($(this).children().attr('href')).offset().top
        }, 800, function(){
            clickScroll = false; //After the animation is finished set the varriable back to false
        });
        return false;
    });

    //Active Link

    $(window).scroll(function () {
        var scrollbarLocation = $(this).scrollTop();
        //console.log(scrollbarLocation);
        $('.nav-link').each(function () {

            var sectionOffset = $($(this).attr('href')).offset().top;

            if (sectionOffset <= scrollbarLocation && !clickScroll) { //ADDED
                $(this).parent().addClass('active');
                $(this).parent().siblings().removeClass('active');
            }
        })
    })

});
$(文档).ready(函数(){
var clickScroll=false;//已添加
$('.nav项')。单击(函数(){
clickScroll=true;//已添加
$(“.nav item”).removeClass('active');
$(this.addClass('active');
$('html,body')。设置动画({
滚动顶部:$($(this.children().attr('href')).offset().top
},800,函数(){
clickScroll=false;//动画完成后,将变量设置回false
});
返回false;
});
//主动链接
$(窗口)。滚动(函数(){
var scrollbarLocation=$(this.scrollTop();
//日志(滚动条位置);
$('.nav link')。每个(函数(){
var sectionOffset=$($(this.attr('href')).offset().top;

如果(sectionOffset您可以添加一个全局变量,以确定是否通过单击或滚动进行滚动:

 $(document).ready(function () {
var clickScroll = false; //ADDED

    $('.nav-item').click(function () {
    clickScroll = true; //ADDED
        $(".nav-item").removeClass('active');
        $(this).addClass('active');
        $('html, body').animate({
            scrollTop: $($(this).children().attr('href')).offset().top
        }, 800, function(){
            clickScroll = false; //After the animation is finished set the varriable back to false
        });
        return false;
    });

    //Active Link

    $(window).scroll(function () {
        var scrollbarLocation = $(this).scrollTop();
        //console.log(scrollbarLocation);
        $('.nav-link').each(function () {

            var sectionOffset = $($(this).attr('href')).offset().top;

            if (sectionOffset <= scrollbarLocation && !clickScroll) { //ADDED
                $(this).parent().addClass('active');
                $(this).parent().siblings().removeClass('active');
            }
        })
    })

});
$(文档).ready(函数(){
var clickScroll=false;//已添加
$('.nav项')。单击(函数(){
clickScroll=true;//已添加
$(“.nav item”).removeClass('active');
$(this.addClass('active');
$('html,body')。设置动画({
滚动顶部:$($(this.children().attr('href')).offset().top
},800,函数(){
clickScroll=false;//动画完成后,将变量设置回false
});
返回false;
});
//主动链接
$(窗口)。滚动(函数(){
var scrollbarLocation=$(this.scrollTop();
//日志(滚动条位置);
$('.nav link')。每个(函数(){
var sectionOffset=$($(this.attr('href')).offset().top;
如果(sectionOffset
将其添加到HTML中
//实际应用偏移量的函数
函数offsetAnchor(){
if(location.hash.length!==0){
scrollTo(window.scrollX,window.scrollY-60);
}
}
//捕获以href开头的所有元素的单击事件#
$(文档)。在('click','a.nav-link[href^=“#”]”上,函数(事件){
//在hashchanges之前捕获单击事件。超时
//导致在页面跳转后调用offsetAnchor。
setTimeout(函数(){
偏移器();
}, 0);
});
//设置输入url中存在哈希的页面时的偏移量
window.setTimeout(offsetAnchor,0);
将此添加到HTML中
//实际应用偏移量的函数
函数offsetAnchor(){
if(location.hash.length!==0){
scrollTo(window.scrollX,window.scrollY-60);
}
}
//捕获以href开头的所有元素的单击事件#
$(文档)。在('click','a.nav-link[href^=“#”]”上,函数(事件){
//在hashchanges之前捕获单击事件。超时
//导致在页面跳转后调用offsetAnchor。
setTimeout(函数(){
偏移器();
}, 0);
});
//设置输入url中存在哈希的页面时的偏移量
window.setTimeout(offsetAnchor,0);
$(文档).ready(函数(){
$('.nav项')。单击(函数(){
$(this).prev().hide('fast').show('fast').hide('fast').show('fast').hide('fast').show('fast');
$(“.nav item”).removeClass('active');
$(this.addClass('active');
$('html,body')。设置动画({
滚动顶部:$($(this.children().attr('href')).offset().top
}, 800);
返回false;
});
//主动链接
$(窗口)。滚动(函数(){
var scrollbarLocation=$(this.scrollTop();
$('.nav link')。每个(函数(){
var sectionOffset=$($(this).attr('href')).offset().top;
如果(截面偏移量
$(文档).ready(函数(){
$('.nav项')。单击(函数(){
$(this).prev().hide('fast').show('fast').hide('fast').show('fast').hide('fast').show('fast');
$(“.nav item”).removeClass('active');
$(this.addClass('active');
$('html,body')。设置动画({
滚动顶部:$($(this.children().attr('href')).offset().top
}, 800);
返回false;
});
//主动链接
$(窗口)。滚动(函数(){
var scrollbarLocation=$(this.scrollTop();
$('.nav link')。每个(函数(){
var sectionOffset=$($(this.attr('href')).offset().top;

如果(sectionOffset我想你可以写一行来解决这个问题

$(window).scroll(function () {
    // ADD
    if ($('html, body').is(':animated')) { return; }
如果是flag,您仍然有问题。 单击导航菜单,然后单击其他导航菜单,直到滚动结束。 我认为最好先停止动画

$('html, body').stop().animate({~

我想你只要写一行就可以解决这个问题

$(window).scroll(function () {
    // ADD
    if ($('html, body').is(':animated')) { return; }
如果是flag,您仍然有问题。 单击导航菜单,然后单击其他导航菜单,直到滚动结束。 我认为最好先停止动画

$('html, body').stop().animate({~

@SajjadTabreez嗯..我试着重现你的问题