javascript - jquery 滚动函数只工作一次

标签 javascript jquery

我有以下代码,应该检测滚动位置并相应地更改类,但由于某种原因它只能工作一次。

    $(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/

相关文章:

javascript - 使用 javascript 淡入淡出主体

javascript - 使用 Jquery Ajax 在 PHP 中动态添加/删除多个输入字段

c# - 我即将开始为我的 Web 应用程序使用 ASP.NET MVC2

jquery - 我可以将 JQuery "this"与后代选择器一起使用吗?

javascript - Flex 容器内的底部对齐 div

javascript - 用逗号匹配字符串 javascript

javascript - jQuery 图没有显示

javascript - 如何替换模态上版本前的追加内容

javascript - 如何使用 jquery 或 javascript 隐藏空的 href 值按钮

javascript - 映射对象键时使通用对象可选(在 keyof 中)