jQuery 为什么对 scrollTop 使用 'html, body' 而不仅仅是 'html'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12222485/
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-26 11:26:41  来源:igfitidea点击:

why to use 'html, body' for scrollTop instead of just 'html'

jquery

提问by Hector Barbossa

I got a solution from stackoverflow to scroll to the top of the page like

我从 stackoverflow 得到了一个解决方案,可以滚动到页面顶部,例如

$('html, body').animate({scrollTop: $("#page").offset().top}, 2000);

But I am not sure why to use 'html, body' for scrollTop instead of just 'html' ?

但我不确定为什么要使用 'html, body' 来代替 scrollTop 而不仅仅是 'html' ?

回答by Niet the Dark Absol

Some browsers apply the "overall" scroll to document.documentElement(the <html>element) and others to document.body(the <body>element). For compatibility with both, you have to apply the scrolling to both.

一些浏览器将“整体”滚动应用于document.documentElement<html>元素)而其他浏览器应用于document.body<body>元素)。为了与两者兼容,您必须将滚动应用于两者。