js之返回网页顶部

目标效果:浏览网页过程中,滑动滚轮,显示返回顶部按钮,点击返回顶部后,返回网页顶端。

代码如下:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 
 7 </head>
 8 <body onscroll="r()">
 9     <div style="height: 1000px;background-color:lavender"></div>
10     <a id="hide" href="#" style="display: none;height: 48px;width: 38px;background-color: bisque;position: fixed;bottom: 10px;right: 10px" onclick="f();">返回顶部</a>
11     <script type="text/javascript">
12         function r() {
13                 document.getElementById('hide').style.display = '';
14         };
15         function f() {
16            document.body.scrollTop = 0;
17         };
18     </script>
19 </body>
20 </html>

 

posted @ 2019-01-12 15:43  飞鸟_山东  阅读(976)  评论(1编辑  收藏  举报