15

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?

1

2 Answers 2

31

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();
Sign up to request clarification or add additional context in comments.

Comments

11

For cross-browser compatibility, use window.pageYOffset

https://developer.mozilla.org/en-US/docs/Web/API/Window/pageYOffset

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.