Google chrome document.body.scrollTop总是返回0 [英] Google chrome document.body.scrollTop always returns 0
问题描述
在Google chrome document.body.scrollTop总是返回0。
我尝试
if(window.pageYOffset> 0){
st = window.pageYOffset;
} else if(document.documentElement.scrollTop> 0){
st = document.documentElement.scrollTop;
} else {
st = document.body.scrollTop;
}
但是不行。
document.body.scrollTop在firefox中工作。
即使在chrome控制台中,当我在控制台中使用此代码时,它也无法正常工作。
在这里输入代码
$('html,body')。stop()。animate({
scrollTop:50
},500);
我也是这样。根据这个问题,这种行为在Chrome 60和Chrome 61之间发生了变化:
https://bugs.chromium.org/p/chromium/issues/detail?id=766938
<从我看过的新行为实际上更符合标准。针对该问题的建议修复方法是:
如果您正在寻找可互操作的查询scrollTop的方式,则可以使用窗口.scrollY属性或者像document.documentElement.scrollTop ||一样document.body.scrollTop
In Google chrome document.body.scrollTop always returns 0.
I try
if (window.pageYOffset > 0) {
st = window.pageYOffset;
} else if (document.documentElement.scrollTop > 0) {
st = document.documentElement.scrollTop;
} else {
st = document.body.scrollTop;
}
But not working. document.body.scrollTop is working in firefox.
Even in chrome console when i this code in console it is not working.
enter code here
$('html, body').stop().animate({
scrollTop: 50
}, 500);
I hit this as well. According to this issue this behavior changed between Chrome 60 and Chrome 61:
https://bugs.chromium.org/p/chromium/issues/detail?id=766938
From what I have read the new behavior is actually more standards compliant. The suggested fix from comment #5 for that issue is:
If you are looking for an interoperable way of querying scrollTop you can use the window.scrollY attribute or doing something like document.documentElement.scrollTop || document.body.scrollTop
这篇关于Google chrome document.body.scrollTop总是返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!