document.body.scrollTop is always 0 in IE even when scrolling

HtmlCss

Html Problem Overview


I am displaying the value of document.body.scrollTop in the status bar while moving the mouse. The value is always 0 in IE. Why is always 0? Is there another way to get how much the scroll bar has moved?

Html Solutions


Solution 1 - Html

You may want to try this for an older doctype in IE:

var top = (document.documentElement && document.documentElement.scrollTop) || 
              document.body.scrollTop;

Solution 2 - Html

this function provides a cross-browser implementation of reading the scroll offset:

function posTop() {
    		return typeof window.pageYOffset != 'undefined' ? window.pageYOffset: document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop? document.body.scrollTop:0;
    	}

Solution 3 - Html

Depending on the DOCTYPE, you would have to use document.body.scrollTop or document.documentElement.scrollTop. Have you tried the second one?

You can do something like this:

var scrollTop = document.documentElement ? document.documentElement.scrollTop :
                                           document.body.scrollTop;

I ran into these links while researching your problem:

This may help you out a little more.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTony_HenrichView Question on Stackoverflow
Solution 1 - HtmlNick CraverView Answer on Stackoverflow
Solution 2 - HtmlijavidView Answer on Stackoverflow
Solution 3 - HtmlVivin PaliathView Answer on Stackoverflow