html
<div class="go" @click="goTop">返回</div>
css
.go {
position: fixed;
right: 5px;
width: 35px;
top: 500px;
border-radius: 50%;
height: 35px;
opacity: 0.53;
background: #000000;
text-align: center;
}
js
goTop() {
let top = document.documentElement.scrollTop || document.body.scrollTop
// 实现滚动效果
const timeTop = setInterval(() => {
document.body.scrollTop = document.documentElement.scrollTop = top -= 50
if (top <= 0) {
clearInterval(timeTop)
}
}, 10)
}
谷歌,火狐,Edge中测试通过,
直接回到顶部
// 滚动到app所在的位置(无滚动效果),如app在顶部,即回到顶部
document.getElementById("app").scrollIntoView();