Chrome 61机身不会滚动

Dav*_*ska 54 javascript scroll google-chrome

有谁知道为什么分配scrollTop身体元素不再有效?

例如: document.body.scrollTop = 200

导致文档不滚动.

原因:Chrome 终于在版本61中符合滚动规范

解决方案:使用scrollingElement

将示例更新为:

var scrollNode = document.scrollingElement ? 
                 document.scrollingElement : document.body;
scrollNode.scrollTop = 200;
Run Code Online (Sandbox Code Playgroud)

Pud*_*601 15

在这个问题的末尾(检查document.scrollingElement或回退document.body)中描述的解决方案将不适用于IE,因为它不支持document.scrollingElement(docs),而在IE中,scroll元素是HTML元素.

因此,我建议对此更好的解决方案是这样的;

var scrollNode = document.scrollingElement || document.documentElement;
Run Code Online (Sandbox Code Playgroud)

哪个适用于所有现代浏览器.


作为一个旁注,它们也同样吸引认为scrollingElement物业似乎是使得它使我们的唯一目的已经添加需要检查/回退为获得根滚动元素,但由于更多的浏览器不兼容,我们仍然需要检查/后备以便使用scrollingElement.

网络开发不是很有趣吗?