Skip to content

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 one 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() { });

User contributions licensed under: CC BY-SA
9 People found this is helpful