Skip to content

Commit f1be7d3

Browse files
committed
add zepto scrollTop module
1 parent 27fba4f commit f1be7d3

File tree

3 files changed

+293
-0
lines changed

3 files changed

+293
-0
lines changed

zepto-scrollTop/demo.html

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>智能社——http://www.zhinengshe.com</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="apple-mobile-web-app-status-bar-style" content="black">
9+
<script src="zepto.min.js"></script>
10+
<script src="zepto-scrolltop.js"></script>
11+
<style>
12+
.btn1{
13+
position: fixed;
14+
right:20px;
15+
bottom:20px;
16+
padding: 20px;
17+
}
18+
</style>
19+
<script>
20+
$(function(){
21+
$('#btn1').on('tap',function(){
22+
$.scrollTop({duration:1000, target:100});
23+
});
24+
$('#btn2').on('tap',function(){
25+
$.scrollTop();
26+
});
27+
});
28+
</script>
29+
</head>
30+
<body style="height:3000px;">
31+
<p>welcome to</p>
32+
<p style="margin-top:100px">welcome to 100</p>
33+
34+
<input class="btn1" type="button" value="回到顶部100" id="btn1">
35+
<input class="btn1" type="button" value="回到顶部0" id="btn2" style="right:140px">
36+
</body>
37+
</html>
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+

zepto-scrollTop/zepto-scrolltop.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Created by strive-智能社 on 2017/2/23.
3+
*/
4+
;(function(_){
5+
_.scrollTop=function(json){
6+
json=json || {};
7+
json.duration=json.duration || 500;
8+
json.target=json.target || 0;
9+
10+
var body=document.body;
11+
clearInterval(body.timer);
12+
var count=Math.floor(json.duration/16);
13+
var start=document.body.scrollTop;
14+
var dis=json.target-start;
15+
16+
var n=0;
17+
body.timer=setInterval(function(){
18+
n++;
19+
20+
var a=1-n/count;
21+
var cur=start+dis*(1-Math.pow(a,3));
22+
23+
document.body.scrollTop=cur;
24+
25+
if(n==count){
26+
clearInterval(body.timer);
27+
}
28+
},16);
29+
};
30+
})($)
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+

0 commit comments

Comments
 (0)