【问题标题】:scrollIntoView not Smooth in EdgescrollIntoView 在边缘不平滑
【发布时间】:2018-07-30 17:52:31
【问题描述】:

我正在使用 vanilla JS 让按钮平滑地向下滚动到 div 元素(以 id 为目标)

我有这个作为我的代码:

this.myButton.addEventListener("click",load_section);
function load_section() {  
        document.querySelector('#myDiv').scrollIntoView({ block: 'end',  behavior: 'smooth' });  
    }

在所有其他浏览器中,这会平滑滚动到该部分。在 Edge 中,它只是跳转到它。我需要更改什么才能使其顺利滚动到该部分

【问题讨论】:

  • Edge 不支持 Afaik 选项对象。
  • 谢谢,刚刚看到,我该怎么做才能启动 Edge 的平滑滚动?

标签: javascript microsoft-edge smooth-scrolling js-scrollintoview


【解决方案1】:

更全面的平滑滚动方法列表见我的回答here。


要在准确的时间内滚动到某个位置,可以使用window.requestAnimationFrame,每次计算适当的当前位置。要滚动到某个元素,只需将 y 位置设置为 element.offsetTop。

/*
   @param pos: the y-position to scroll to (in pixels)
   @param time: the exact amount of time the scrolling will take (in milliseconds)
*/
function scrollToSmoothly(pos, time) {
    var currentPos = window.pageYOffset;
    var start = null;
    if(time == null) time = 500;
    pos = +pos, time = +time;
    window.requestAnimationFrame(function step(currentTime) {
        start = !start ? currentTime : start;
        var progress = currentTime - start;
        if (currentPos < pos) {
            window.scrollTo(0, ((pos - currentPos) * progress / time) + currentPos);
        } else {
            window.scrollTo(0, currentPos - ((currentPos - pos) * progress / time));
        }
        if (progress < time) {
            window.requestAnimationFrame(step);
        } else {
            window.scrollTo(0, pos);
        }
    });
}

演示:

function scrollToSmoothly(pos, time) {
    var currentPos = window.pageYOffset;
    var start = null;
    if(time == null) time = 500;
    pos = +pos, time = +time;
    window.requestAnimationFrame(function step(currentTime) {
        start = !start ? currentTime : start;
        var progress = currentTime - start;
        if (currentPos < pos) {
            window.scrollTo(0, ((pos - currentPos) * progress / time) + currentPos);
        } else {
            window.scrollTo(0, currentPos - ((currentPos - pos) * progress / time));
        }
        if (progress < time) {
            window.requestAnimationFrame(step);
        } else {
            window.scrollTo(0, pos);
        }
    });
}

document.getElementById("toElement").addEventListener("click", function(e){
  scrollToSmoothly(document.querySelector('div').offsetTop, 500 /* milliseconds */);
});
document.getElementById("backToTop").addEventListener("click", function(e){
  scrollToSmoothly(0, 500);
});
<button id="toElement">Scroll To Element</button>
<div style="margin: 1000px 0px; text-align: center;">Div element
  <button id="backToTop">Scroll back to top</button>
</div>

也可以使用SmoothScroll.js library,它支持滚动到页面上的一个元素,除了更复杂的功能,例如垂直和水平平滑滚动,在其他容器元素内滚动,不同的缓动行为,相对滚动当前位置等。

smoothScroll({toElement: document.getElementById('elementId'), duration: 500});

document.getElementById("toElement").addEventListener("click", function(e){
  smoothScroll({toElement: document.querySelector('div'), duration: 500});
});
document.getElementById("backToTop").addEventListener("click", function(e){
  smoothScroll({yPos: 'start', duration: 500});
});
<script src="https://cdn.jsdelivr.net/gh/LieutenantPeacock/SmoothScroll@1.2.0/src/smoothscroll.min.js" integrity="sha384-UdJHYJK9eDBy7vML0TvJGlCpvrJhCuOPGTc7tHbA+jHEgCgjWpPbmMvmd/2bzdXU" crossorigin="anonymous"></script>
<button id="toElement">Scroll To Element</button>
<div style="margin: 1000px 0px; text-align: center;">Div element
  <button id="backToTop">Scroll back to top</button>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多