【问题标题】:scroll not firing on consecutive div elements滚动不会在连续的 div 元素上触发
【发布时间】:2014-03-13 04:45:57
【问题描述】:

我是新来的……

我有这个 html 标记:

... 很长的内容;也被动态附加
... 很长的内容;也被动态附加

并且,有以下脚本sn-p:

$(document).ready(function () {
    $('.scrollMe').scroll(function () {
        // do something
    });
});

我的问题是,滚动只会在第一个 DIV 容器上触发,而不会在任何连续的 DIV 上触发。我已经尝试了所有我能想到的方法,包括“.on”,但没有任何效果。有人知道另一种方法吗?谢谢。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    两者都很完美

    检查这个jsFiddle

    jQuery

    $(document).ready(function () {
      $('.scrollMe').scroll(function () {
        // do something
      });
    });
    

    【讨论】:

      【解决方案2】:

      查看我的代码后,我发现了问题所在(请参阅下面的更多详细信息):

      $(document).ready(function () {
      $('.scrollMe').scroll(function () {
          // do something
          currentid = $('.scrollMe').attr('id');  
      
          // = "1" <--- no matter which container is scrolled, will always grab the 1st 
          alert(currentid);   
      
          // do more stuff
      });
      });
      

      既然我们有不同的DIV滚动,应该这样写:

      $(document).ready(function () {
      $('.scrollMe').scroll(function () {
          // do something
          // just an illustration 
      
          currentid = $(this).attr('id');
      
          alert(currentid);
      
          // do more stuff
      });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-31
        • 2018-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多