javascript body.scrollTop 在严格模式下已弃用。请在严格模式下使用“documentElement.scrollTop”,仅在 quirks 模式下使用“body.scrollTop”。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21268450/
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
body.scrollTop is deprecated in strict mode. Please use 'documentElement.scrollTop' if in strict mode and 'body.scrollTop' only if in quirks mode.
提问by sam
I'm receiving the error:
我收到错误:
body.scrollTop is deprecated in strict mode. Please use 'documentElement.scrollTop' if in strict mode and 'body.scrollTop' only if in quirks mode.
body.scrollTop 在严格模式下已弃用。请在严格模式下使用“documentElement.scrollTop”,仅在 quirks 模式下使用“body.scrollTop”。
My code is:
我的代码是:
$(document).ready(function(){
//Animates Scrolling to anchor
function scrollToAnchor(aid){
var divTag = $("div[name='"+ aid +"']");
$('html,body').animate({scrollTop: divTag.offset().top},'slow');
}
//If Checking out as guest, scroll to Shipping Information
$("#ReadDescription").click(function() {
scrollToAnchor('longdescreadmore');
});
});
How can I edit my code to use this documentElement.ScrollTop?
如何编辑我的代码以使用此 documentElement.ScrollTop?
回答by sam
Dagg Nabbit gave the solution. Change
达格·纳比特给出了解决方案。改变
$('html,body').animate({scrollTop: divTag.offset().top},'slow');
to
到
$('html').animate({scrollTop: divTag.offset().top},'slow');
if you want to avoid the deprecation warning in Chrome. (Why is body.scrollTop
deprecated?)
如果您想避免 Chrome 中的弃用警告。(为什么被body.scrollTop
弃用?)
It works because documentElement
is the html
node:
它的工作原理documentElement
是html
节点:
$('html')[0] === document.documentElement //-> true
$('body')[0] === document.body //-> true
But your code is working now (albeit with a warning) and it will keep working when Chrome removes the "quirky" behavior. You shouldn'tchange your code if you want to continue supporting browsers that use body.scrollTop
to represent the scrolling viewport in standards mode (older Chrome and Safari, I think).
但是您的代码现在正在运行(尽管有警告)并且当 Chrome 删除“古怪”行为时它将继续运行。你不应该,如果你想继续支持使用浏览器更改您的代码body.scrollTop
来表示的标准模式(旧的Chrome和Safari浏览器,我认为)滚动视口。