Html 即使在滚动时,文档.body.scrollTop 在 IE 中也始终为 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2717252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:51:07  来源:igfitidea点击:

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

htmlcss

提问by Tony_Henrich

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?

我在移动鼠标时在状态栏中显示 document.body.scrollTop 的值。该值在 IE 中始终为 0。为什么总是0?有没有另一种方法来获取滚动条移动了多少?

回答by Nick Craver

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

您可能想在 IE 中尝试使用较旧的文档类型:

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

回答by ijavid

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;
        }

回答by Vivin Paliath

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

根据 DOCTYPE,您必须使用document.body.scrollTopdocument.documentElement.scrollTop。你试过第二种吗?

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.

这可能会对您有所帮助。