window.scrollTo选项在Microsoft Edge上不起作用 [英] window.scrollTo with options not working on Microsoft Edge

查看:882
本文介绍了window.scrollTo选项在Microsoft Edge上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a strange issue which I can only replicate on Microsoft browsers (Edge and IE11 tested).

<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>

此代码正确地将窗口向左和向下滚动1000px,在Chrome和Firefox中行为流畅.但是,在Edge和IE上,它根本不会移动.

This code correctly scrolls the window 1000px to the left and down, with a smooth behaviour in Chrome and Firefox. However, on Edge and IE, it does not move at all.

推荐答案

从字面上看,这可能不是一个正确的答案,但是我已经通过使用以下有用的polyfill解决了这个问题:

Maybe not a true answer in the sense of the word, but I have solved this problem by using this helpful polyfill: https://github.com/iamdustan/smoothscroll which works really well across all browsers.

polyfill的示例页面: http://iamdustan.com/smoothscroll/

Example page for pollyfill: http://iamdustan.com/smoothscroll/

非常感谢作者.

这篇关于window.scrollTo选项在Microsoft Edge上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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