👦

 

jquery实现返回顶部功能

功能代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<script src='jquery-1.12.4.js'></script>
<style>
body{
min-height: 1000px;
background: skyblue;
}
#dv{
width: 40px;
height: 40px;
background-color: springgreen;
position: fixed;
right: 10px;
bottom: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="dv"></div>
<script>
$(function(){
$("#dv").click(function(){
$("html,body").animate({scrollTop:0});
});
});
</script>
</body>
</html>

简要分析:

首先要引入一个jQuery的js库,然后写入一个div并给予样式使其固定在了页面右下角,然后通过点击事件触发让页面返回顶部,当前通过动画函数的方式控制html和body,使其scrollTop为0即可。