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?
A:
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:
- Window size and scrolling (towards the bottom)
- document.body.scrollTop in IE
This may help you out a little more.
Vivin Paliath
2010-04-26 22:06:52
+1
A:
You may want to try this for an older doctype in IE:
var top = document.documentElement.scrollTop ?
document.documentElement.scrollTop :
document.body.scrollTop;
Nick Craver
2010-04-26 22:09:28
Wouldn't that bomb if `document.documentElement` is undefined? I think you meant `document.documentElement` instead of `document.documentElement.scrollTop` in the first part of the ternary expression. :)
Vivin Paliath
2010-04-26 22:11:28
@Vivin - It's not undefined, the scroll property just isn't set, resulting in 0/false.
Nick Craver
2010-04-26 22:13:58