问题描述
似乎 body.scrollTop
(以及 body.scrollLeft
)在ES5严格模式下已弃用。这是什么原因,因为在其他 DOMElement
s上使用这些属性似乎还可以吗?
It seems body.scrollTop
(and body.scrollLeft
) are deprecated in ES5 strict-mode. What is the reason for this, given that it still seems okay to use these properties on other DOMElement
s?
背景信息:
我有一个函数试图增加(或减少,如指定) scrollTop
的值元素的所有祖先
,直到其中一个实际发生变化。我想知道,为了保持对严格模式的投诉,我应该专门检查正文
元素,因为父母链向上移动。
I have a function that tries to increase (or decrease, as specified) the scrollTop
values of all the ancestors of an element
, till one of these actually changes. I am wondering if, to stay complaint with strict-mode, I should specifically check against the body
element as the chain of parents moves upward.
[显然,正文
指的是 document.body
]
推荐答案
这是Chrome自身不正确的行为,已被弃用,他们警告作者不再依赖它。
It's Chrome's own incorrect behavior that is deprecated, and they're warning authors to stop relying on it.
。 (模拟Navigator 4和Explorer 5的文档呈现。)
The scrolling viewport is represented by document.documentElement
(<html>
) in standards mode or <body>
in quirks mode. (Quirks mode emulates the document rendering of Navigator 4 and Explorer 5.)
Chrome使用 body.scrollTop
来表示视口在两种模式下的滚动位置是错的。这听起来像,以便他们鼓励作者为标准行为编写脚本。
Chrome uses body.scrollTop
to represent the viewport's scroll position in both modes, which is wrong. It sounds like they want to fix this so they're encouraging authors to script for the standard behavior.
我认为您不需要更改代码。在标准模式下使用 body.scrollTop
没有任何问题,只要您了解它只代表 body
的滚动位置(通常 0
,除非你给了 body
一个滚动框)。
I don't think you need to change your code. There's nothing wrong with using body.scrollTop
in standards mode so long as you understand it represents the scroll position of body
only (typically 0
, unless you've given body
a scroll box).
您可以通过在控制台中执行 document.body.scrollTop
来查看警告:
You can see the warning by executing document.body.scrollTop
in the console:
这篇关于为什么不推荐使用body.scrollTop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!