1、纯js,无动画版本
window.scrollTo(x-coord, y-coord);
window.scrollTo(0,0);
2、纯js,带动画版本
1.
var scrollToTop = window.setInterval(function() {
var pos = window.pageYOffset;
if ( pos > 0 ) {
window.scrollTo( 0, pos - 20 ); // how far to scroll on each step
} else {
window.clearInterval( scrollToTop );
}
}, 16); // how fast to scroll (this equals roughly 60 fps)
(function smoothscroll(){
var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
if (currentScroll > 0) {
window.requestAnimationFrame(smoothscroll);
window.scrollTo (0,currentScroll - (currentScroll/5));
}
})();
3、vue element UI 表格table 纯js,带动画版本
设置element-ui table 点击分页table滚到顶部
设置table的 ref='multipleTable'
//切换分页的方法加上下面这句,table就能自动滚到顶部
this.$refs.multipleTable.bodyWrapper.scrollTop = 0;