document.documentElement.scrollTop return value differs in Chrome

I am trying to process some code based on the ‘document.documentElement.scrollTop‘ value. It returns ‘348‘ in FF and IE but in Chrome it returns ‘0‘. Do i need to do anything to overcome this issue?

FF:

>>> document.documentElement.scrollTop
342

Chrome:

document.documentElement.scrollTop
0

39 thoughts on “document.documentElement.scrollTop return value differs in Chrome”

  1. 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

    Reply
  2. 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)

    Reply
  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:

    typeof window.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.

    function registerScrollHandler (callback) {
        "use strict"
    
        var useLegacyScroll = typeof window.scrollY === "undefined",
            lastX = useLegacyScroll ? window.pageXOffset : window.scrollX,
            lastY = useLegacyScroll ? window.pageYOffset : window.scrollY
    
        function scrollHandler () {
            // get the values using legacy scroll if we need to
            var thisX = useLegacyScroll ? window.pageXOffset : window.scrollX,
                thisY = useLegacyScroll ? window.pageYOffset : window.scrollY
    
            // if either the X or Y scroll position changed
            if (thisX !== lastX || thisY !== lastY) {
                callback(thisX, thisY)
    
                // save the new position
                lastX = thisX
                lastY = thisY
            }
    
            // check again on the next frame
            window.requestAnimationFrame(scrollHandler)
        }
    
        scrollHandler()
    }
    

    Use the function like this:

    registerScrollHandler(function (x, y) {
        /* your code here :) */
        console.log("Scrolled the page", x, y)
    })
    
    Reply

Leave a Comment