页面平滑滚动
interlocutionDetail
为父元素类名,整体移动也可以换为body
backToTop(){ //平滑返回页面顶部
let total = document.querySelector('.interlocutionDetail').offsetTop-50 //这里的50是我头部header的高度
let distance = document.documentElement.scrollTop || document.body.scrollTop; //获取需要滚动的距离
// 平滑滚动,时长500ms,每10ms一跳,共50跳
let step = total / 50
if (total > distance) {
smoothDown()
} else {
let newTotal = distance - total
step = newTotal / 50
smoothUp()
}
function smoothUp () {
if (distance > total) {
distance -= step
window.scrollTo(0,distance)
setTimeout(smoothUp, 10)
} else {
window.scrollTo(0,total)
}
}
}