This question already has an answer here:
Use the source:
var scrollPosition = window.pageYOffset;
document.body.scrollTop doesn’t work (is always 0) in Firefox; it’s
document.documentElement.scrollTop if you want to use a
scrollTop at all. Conversely,
document.documentElement.scrollTop is always 0 in Chrome (well, Blink and WebKit).
window.scrollY doesn’t exist in IE 8 and earlier.
So, for old IE compatibility, use:
var scrollPosition = 'pageYOffset' in window ? window.pageYOffset : document.body.scrollTop;