document.body.scrollTop Firefox returns 0 : ONLY JS

前端 未结 3 1305
清酒与你
清酒与你 2020-11-27 04:36

Any alternatives in pure javascript?

The following works in opera, chrome and safari. Have not tested yet on explorer:

http://monkey-me.herokuapp.com

3条回答
  •  旧时难觅i
    2020-11-27 04:59

    The standard is document.documentElement and this is used by FF and IE.

    WebKit uses document.body and couldn't use the standard because of complaints about backward compatibility if they changed to the standard, this post explains it nicely

    https://miketaylr.com/posts/2014/11/document-body-scrolltop.html

    There is a new property on document which WebKit now supports

    https://developer.mozilla.org/en/docs/Web/API/document/scrollingElement

    so this will get you to the right element

    var scrollingElement = document.scrollingElement || document.documentElement;
    scrollingElement.scrollTop = 100;
    

    and there is a polyfill too

    https://github.com/mathiasbynens/document.scrollingElement

提交回复
热议问题