To check the scroll values of the page I used to wrap the window
object with jQuery, but when scrolling, the target element of the scroll event results to be the document
object:
$(window).scroll(function(e) {
alert('Scrolling ' + e.target);
});
What is the right object to wrap to check the scroll values?
I know the difference between them (a window can contain multiple frames thus many documents), but for a single document context I see the scroll values coincide:
alert($(window).scrollTop() === $(document).scrollTop());
EDIT:
It also happens with native JavaScript:
window.onscroll = function(e) { alert('scrolled ' + e.target); };
The element bound is window
, but the event target is document
.
About the expression written above, comparing the scrollTop value of the window
object with the one of the document
object: the jQuery documentation explains that $(window).width()
returns the width of the viewport, while $(document).width()
returns the width of the HTML DOM element; since the viewport may be smaller than the whole HTML element width, these two values may differ.