Is there any difference between those two?
$(window).on('scroll', function() { /**/ });
and
$(document).on('scroll', function() { /**/ });
The reason I'm asking is that I've noticed that if I have 2 handlers (one listening on window
and the second on document
) and at some point I will return false;
in the document
one, the window
handler stops firing, even if the event is namespaced.
You can see the behavior here. After you scroll beyond 300px from the top, the window
handler's count doesn't increase anymore.
If I change the window
selector to document
, the conflict doesn't occur.
And the Fiddle code:
HTML
<div class="fixed">
<div>document scroll event: <span class="scroll1">0</span></div>
<div>window scroll event: <span class="scroll2">0</span></div>
</div>
CSS
body {
height: 3000px;
}
.fixed {
position: fixed;
top: 20px;
left: 20px;
}
JS
$(function () {
var $scroll1 = $('.scroll1');
var $scroll2 = $('.scroll2');
function updateCount($el) {
$el.text(parseInt($el.text(), 10) + 1);
}
$(document).on('scroll', function () {
updateCount($scroll1);
if ($(this).scrollTop() > 300) {
return false;
}
});
$(window).on('scroll', function () {
updateCount($scroll2);
});
});