I’m trying to target the scroll event for both the window and scrollable divs. Is there a way to do this in one statement?
I’ve tried…
JavaScript
x
2
1
$(window, '.box-scroll').scroll(function() { });
2
Only way I have found is calling them both separately…
JavaScript
1
3
1
$(window).scroll(function() { });
2
$('.box-scroll').scroll(function() { });
3
Advertisement
Answer
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:
JavaScript
1
3
1
var $d = $($.map([$(window), $('.boxscroll')], function(el){return $.makeArray(el)}));
2
$d.on('scroll', function() { });
3
EDIT: $(window).add('.box-scroll').scroll(function() { });