简单实现点击回到顶部

var btn = document.getElementById('btn');
        var timer = null;
        btn.onclick = function () {
            timer = window.setInterval(() => {
                // document.documentElement.csrollTop = 0;
                if (document.documentElement.scrollTop == 0) {
                    clearInterval(timer);
                    return
                }
                document.documentElement.scrollTop -= 20;
            }, 5)
        }
        window.onmousewheel = function () {//鼠标滚轮事件
            console.log(document.documentElement.scrollTop)
            clearInterval(timer);
        }

复制代码
相关文章
相关标签/搜索