简体   繁体   中英

scroll eevent and scrollTop not working in firefox

I want to get the scrollTop value on scroll event.

Works good on Chrome but not on firefox. What is the way to get this, crossbrowser/vanilla JS?

var i = 0;
window.onscroll = function () {
    var scrollTop = document.body.scrollTop;
    console.log(scrollTop,i); // always 0, (i works as expected)
    i++;
};

Fiddle

Ps- I don't think a setTimeout is a good practise/solution for this...

var i = 0;
var doc = document;
window.onscroll = function () {
    var scrollTop = doc.documentElement.scrollTop || doc.body.scrollTop;
    console.log(scrollTop,i);
    i++;
};

https://developer.mozilla.org/en-US/docs/Web/API/document.documentElement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM