I would like to retrieve the current scroll position in Javascript, after some research I found this: window.scrollY
and window.scollTop
.
But the problem is that it does not work 100% on all browsers, is there something more reliable?
The solution for JavaScript is:
var scrollPos = window.scrollY || window.scrollTop || document.getElementsByTagName("html")[0].scrollTop;
Or if you use jQuery (this is more reliable, due cross-browser support):
var scrollPos = $(window).scrollTop();
For cross-browser compatibility, use
window.pageYOffset
https://developer.mozilla.org/en-US/docs/Web/API/Window/pageYOffset
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With