解决jquery animate({scrollTop$pos},500)与$(window).scroll方法冲突的问题

当点击节点时 先移除$(window).on("scroll")监听事件 在animate动画结束之后再添加上

 $('#J_tab li').on('click', function(){

             $(window).off(".changeCityActive");

             $('html,body').animate({scrollTop:$('.list-item-wrp[data-city="'+cityName+'"]:first').offset().top-offTop}, 400, 'easeOutExpo', function(){
$(window).on("scroll.changeCityActive" , changeCityActive);
});
}); $(window).on("scroll.changeCityActive" , changeCityActive); function changeCityActive(){
var shTop = $('.list-item-wrp[data-city="shanghai"]:first').offset().top;
var hzTop = $('.list-item-wrp[data-city="hangzhou"]:first').offset().top;
var szTop = $('.list-item-wrp[data-city="suzhou"]:first').offset().top;
var njTop = $('.list-item-wrp[data-city="nanjing"]:first').offset().top;
var qtTop = $('.list-item-wrp[data-city="qita"]:first').offset().top; if ($("#rank-wrp").css("position") == "fixed" && $(window).scrollTop() < hzTop -130) { $("#J_tab li[data-city='shanghai']").addClass('active').siblings().removeClass('active');
}else if ($(window).scrollTop() >= hzTop-130 && $(window).scrollTop() < szTop-130){ $("#J_tab li[data-city='hangzhou']").addClass('active').siblings().removeClass('active');
}else if ($(window).scrollTop() >= szTop-130 && $(window).scrollTop() < njTop-130){ $("#J_tab li[data-city='suzhou']").addClass('active').siblings().removeClass('active');
}else if($(window).scrollTop() >= njTop-130 && $(window).scrollTop() < qtTop-130){ $("#J_tab li[data-city='nanjing']").addClass('active').siblings().removeClass('active');
}else if($(window).scrollTop() > qtTop-130){ $("#J_tab li[data-city='qita']").addClass('active').siblings().removeClass('active');
}
}
上一篇:YII2.0学习一 Advanced 模板安装


下一篇:elasticsearch.in.sh优化内存