简体   繁体   中英

jQuery target window and a scrollable div?

I'm trying to target the scroll event for both the window and scrollable divs. Is there a way to do this in on statement?

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:

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

EDIT: $(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'); };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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