如何在 IE 11 中获取当前的 ScrollY 位置

How to get the current ScrollY position in IE 11

我正在尝试为网页构建一个从一个位置平滑滑动到下一个位置的滚动条。 (想想触摸设备,例如:- Android 手机)。我设法制作了一个,它在 Chrome 中运行得非常好。但问题是 IE 11。

IE 11 中没有window.scrollY。 有什么方法可以在 IE(9 及更高版本)中找到滚动条的当前 Y 位置?

您应该可以使用 document.body.scrollTop 找到它。要使其向后(或 IE)兼容,试试这个:

var scrollTop = window.scrollY || document.body.scrollTop || 0;

然后使用 scrollTop 作为你的变量。

使用document.documentElement.scrollTop

根据MDN web docs scrollTop works this way with the root element. And the root element in the browsers is referenced by document.documentElement (referece).

这应该适用于 IE 和现代浏览器。