我有以下代码,应该检测滚动位置并相应地更改类,但由于某种原因它只能工作一次。
$(document).on('scroll',function() {
// Do something
var scroll = $(window).scrollTop();
if (scroll >= 70)
{
if ($('#head-nav').hasClass('off') ) {
$('#head-nav').removeClass('top clearfix off');
$('#head-nav').addClass('top clearfix on');
$('#cart').removeClass('cart-on');
$('#cart').addClass('cart-off');
$('#nav-menu').removeClass('navigation grid-40 pull-30 tablet-grid-25 tablet-pull-25 omega alpha');
$('#nav-menu').addClass('navigation grid-40 pull-0 tablet-grid-25 tablet-pull-25 omega alpha');
//$('#cart').css('dislay','none !important');
}
}
else if(scroll < 70)
{
if ($('#head-nav').hasClass('on') ) {
$('#cart').removeClass('cart-off');
$('#cart').addClass('cart-on');
$('#head-nav').removeClass('top clearfix on');
$('#head-nav').removeClass('top clearfix off');
$('#nav-menu').removeClass('navigation grid-40 pull-0 tablet-grid-25 tablet-pull-25 omega alpha');
$('#nav-menu').addClass('navigation grid-40 pull-30 tablet-grid-25 tablet-pull-25 omega alpha');
}
}
});
你可以查看我的代码 here
最佳答案
滚动功能正常工作...您没有将类 off 添加回 $('#head-nav')...我猜这一行有错误:
$('#head-nav').removeClass('top clearfix on');
$('#head-nav').removeClass('top clearfix off'); //? add class not remove?
关于javascript - jquery 滚动函数只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25286943/