js实现返回页面顶部

无动态效果

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事件的函数,或者执行函数,就能执行,返回顶部。

其他

[我的博客,欢迎交流!](http://rattenking.gitee.io/stone/index.html)

[我的CSDN博客,欢迎交流!](https://blog.csdn.net/m0_38082783)

[微信小程序专栏](https://blog.csdn.net/column/details/18335.html)

[前端笔记专栏](https://blog.csdn.net/column/details/18321.html)

[微信小程序实现部分高德地图功能的DEMO下载](http://download.csdn.net/download/m0_38082783/10244082)

[微信小程序实现MUI的部分效果的DEMO下载](http://download.csdn.net/download/m0_38082783/10196944)

[微信小程序实现MUI的GIT项目地址](https://github.com/Rattenking/WXTUI-DEMO)

[微信小程序实例列表](http://blog.csdn.net/m0_38082783/article/details/78853722)

[前端笔记列表](http://blog.csdn.net/m0_38082783/article/details/79208205)

[游戏列表](http://blog.csdn.net/m0_38082783/article/details/79035621)

 

posted @ 2017-06-23 16:48  Newman·Li  阅读(1757)  评论(0编辑  收藏  举报