简体   繁体   English

jQuery目标窗口和可滚动div?

[英]jQuery target window and a scrollable div?

I'm trying to target the scroll event for both the window and scrollable divs. 我正在尝试针对窗口和可滚动div的滚动事件。 Is there a way to do this in on statement? 是否可以在on语句中执行此操作?

I've tried... 我试过了...

$(window, '.box-scroll').scroll(function() { });

Only way I have found is calling them both separately... 我发现的唯一方法是将它们分别称为...

$(window).scroll(function() { });
$('.box-scroll').scroll(function() { });

There may be a better way to do this, but you could use $.map to create a jquery object with both window and .boxscroll, like so: 也许有更好的方法,但是您可以使用$ .map来创建同时具有window和.boxscroll的jquery对象,如下所示:

var $d = $($.map([$(window), $('.boxscroll')], function(el){return $.makeArray(el)}));
$d.on('scroll', function() { ... });

EDIT: $(window).add('.box-scroll').scroll(function() { }); 编辑: $(window).add('.box-scroll').scroll(function() { });

Fiddled with it for a while but can't see any reason it wouldn't. 摆弄了一段时间,但看不到任何理由。

I can suggest a workaround at best, in case you just need a single function. 如果您只需要一个功能,我最多可以建议一种解决方法。

  $(window).scroll(function(){scroller('a');});
  $('.box-scroll').scroll(function(){scroller('b');});

  function scroller(source){$('.box-scroll-inside').html('scrollling'); };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM