gpt4 book ai didi

javascript - window.scrollTo 选项不适用于 Microsoft Edge

转载 作者:行者123 更新时间:2023-12-04 01:02:36 27 4
gpt4 key购买 nike

我有一个奇怪的问题,我只能在 Microsoft 浏览器上复制(Edge 和 IE11 测试)。

<style>
body {
height: 5000px;
width: 5000px;
}
</style>
<p>Click the button to scroll the document window to 1000 pixels.</p>
<button onclick="scrollWin()">Click me to scroll!</button>
<script>
function scrollWin() {
window.scrollTo({
left: 1000,
top: 1000,
behavior:"smooth"
});
}
</script>


此代码正确地将窗口向左和向下滚动 1000 像素,在 Chrome 和 Firefox 中具有流畅的行为。但是,在 Edge 和 IE 上,它根本不动。

最佳答案

如前所述,Scroll Behavior specification仅在 Chrome、Firefox 和 Opera 中实现。
这是检测对 behavior 的支持的单行代码。位于 ScrollOptions 的属性(property):

const supportsNativeSmoothScroll = 'scrollBehavior' in document.documentElement.style;
这是跨浏览器平滑滚动的简单实现: https://gist.github.com/eyecatchup/d210786daa23fd57db59634dd231f341

关于javascript - window.scrollTo 选项不适用于 Microsoft Edge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276194/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com