为什么不推荐使用body.scrollTop? [英] Why is body.scrollTop deprecated?

查看:173
本文介绍了为什么不推荐使用body.scrollTop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 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 DOMElements?

背景信息:

我有一个函数试图增加(或减少,如指定) 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.

滚动视口由<$ c表示标准模式下的$ c> document.documentElement (< html> )或< body> 在怪癖模式下。 ( Quirks模式模拟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 在严格模式下已弃用。如果处于严格模式,请使用 documentElement.scrollTop ;如果处于怪癖模式,请使用 body.scrollTop

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆