Document.documentelement.scrolltop Return Value Differs In Chrome
Solution 1:
The standards-based way of getting the scroll is window.scrollY
. This is supported by Chrome, Firefox, Opera, Safari and IE Edge or later. If you only support these browsers, you should go with this property.
IE >= 9 supports a similar property window.pageYOffset
, which for the sake of compatibility returns the same as window.scrollY
in recent browsers, though it may perhaps be deprecated at some point.
The problem with using document.documentElement.scrollTop
or document.body.scrollTop
is that the scroll needn't be defined on either of these. Chrome and Safari define their scroll on the <body>
element whilst Firefox defines it on the <html>
element returned by document.documentElement
, for example. This is not standardized, and could potentially change in future versions of the browsers. However, if the scrollY
or pageYOffset
are not present, this is the only way to get the scroll.
TL;DR:
window.scrollY || window.pageYOffset || document.body.scrollTop + (document.documentElement && document.documentElement.scrollTop || 0)
Solution 2:
Try this
window.pageYOffset || document.documentElement.scrollTop
Solution 3:
Use window.scrollY where possible, it's designed to be consistent across browsers. If you need to support IE, then I'd recommend the following to only use window.scrollY
if it's available:
typeofwindow.scrollY === "undefined" ? window.pageYOffset : window.scrollY
window.scrollY
will be evaluated as false if it returns 0, so doing window.scrollY || window.pageYOffset
would technically check window.pageYOffset
whenever window.scrollY
were 0, which obviously isn't ideal if window.pageYOffset
did not also equal 0.
Also note that if you need to get the scroll value frequently (every frame/every scroll) as is often the case, you might want to check if window.scrollY
is defined beforehand. I like to use this small helper function I wrote to do exactly that, along with using requestAnimationFrame
- it should work in IE10 and up.
functionregisterScrollHandler (callback) {
"use strict"var useLegacyScroll = typeofwindow.scrollY === "undefined",
lastX = useLegacyScroll ? window.pageXOffset : window.scrollX,
lastY = useLegacyScroll ? window.pageYOffset : window.scrollYfunctionscrollHandler () {
// get the values using legacy scroll if we need tovar thisX = useLegacyScroll ? window.pageXOffset : window.scrollX,
thisY = useLegacyScroll ? window.pageYOffset : window.scrollY// if either the X or Y scroll position changedif (thisX !== lastX || thisY !== lastY) {
callback(thisX, thisY)
// save the new position
lastX = thisX
lastY = thisY
}
// check again on the next framewindow.requestAnimationFrame(scrollHandler)
}
scrollHandler()
}
Use the function like this:
registerScrollHandler(function (x, y) {
/* your code here :) */console.log("Scrolled the page", x, y)
})
Solution 4:
You can just use the following codes to fix that bug!
let scrollHeight = document.body.scrollTop || document.documentElement.scrollTop;
console.log(`scrollHeight = ${scrollHeight}`);
/*
this comment just using for testing the scroll height!
but in this iframe, it doesn't work at all!
So, you can try it out using Chrome console!
*/
document.body.scrollTop; // For Chrome, Safari and Opera document.documentElement.scrollTop; // Firefox and IE places the overflow at the level, unless else is specified. Therefore, we use the documentElement property for these two browsers
reference links:http://www.w3schools.com/jsref/prop_element_scrolltop.asp
https://drafts.csswg.org/cssom-view/#dom-element-scrolltop
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop
Solution 5:
You can use this function
document.body.getBoundingClientRect()
and it returns this object {x: 0, y: 0, width: 1903, height: 2691.5625, top: 0, …};
in this object you can access body top document.body.getBoundingClientRect().top
Post a Comment for "Document.documentelement.scrolltop Return Value Differs In Chrome"