📜  js动画滚动到页面顶部 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:03:16.643000             🧑  作者: Mango

代码示例2
function scrollTo(element, to, duration) {
    if (duration <= 0) return;
    var difference = to - element.scrollTop;
    var perTick = difference / duration * 10;

    setTimeout(function() {
        element.scrollTop = element.scrollTop + perTick;
        if (element.scrollTop === to) return;
        scrollTo(element, to, duration - 10);
    }, 10);
}