无动态效果
1、原生JS
window.scrollTo(0,0);
2、jQuery
$(window).scrollTop(0);
有动态效果
1、原生JS
function smoothscroll(){
window.requestAnimationFrame(smoothscroll);
window.scrollTo (0,currentScroll - (currentScroll/5));
}
2、jQuery
$('html,body').animate({scrollTop:0},200);
if($('html').scrollTop()){
$('html').animate({scrollTop:0},200);
return false;
}
$('body').animate({scrollTop:0},200);
return false;
注意:将代码放入click事件的函数,或者执行函数,就能执行,返回顶部。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有