返回顶部是我们常用的插件,和大家分享一下之前封装的一个。
export default{
install(Vue){
Vue.prototype.$scrollTo = function(end=0,time=100,cb){
var start = document.documentElement.scrollTop || document.body.scrollTop;
var speed = (start-end)/100*16.7;
var timer = setInterval(() => {
document.documentElement.scrollTop = document.body.scrollTop = start -= speed;
if(start <= end && speed>0 ){
clearInterval(timer);
cb?cb():null
}else if(start >= end && speed<0){
clearInterval(timer);
cb?cb():null
}
}, 16.7);
}
}
}