This repository has been archived by the owner on Jan 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
snapping.html
113 lines (94 loc) · 2 KB
/
snapping.html
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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Snapping</title>
<style>
html, body {
padding:0;
margin:0;
width:100%;
height:100%;
}
body {
overflow:auto;
}
h1 {
margin:0;
padding:0;
padding-top:2em;
font-family:sans-serif;
color:#fff;
}
div {
width:100%;
height:100%;
background:#09f;
text-align:center;
}
.odd {
background:#f90;
}
</style>
</head>
<body style="text-align:center;">
<div id="start">
<h1>The different parts of this webpage will snap after you scroll</h1>
</div>
<div class="odd" id="a">
<h1>A</h1>
</div>
<div id="b">
<h1>B</h1>
</div>
<div class="odd" id="c">
<h1>C</h1>
</div>
<div id="d">
<h1>D</h1>
</div>
<script type="text/javascript" src="../dist/skrollr.min.js"></script>
<script type="text/javascript">
var layers = document.getElementsByTagName('div');
var activeLayer = 0;
var animateUpSettings = {
duration: 200,
done: function() {
document.body.style.overflow = 'auto';
}
};
var animateDownSettings = {
duration: 2000,
easing: 'sqrt',
done: animateUpSettings.done
};
var s = skrollr.init({
render: function(info) {
if(this.isAnimatingTo()) {
return;
}
var lastOffset = this.relativeToAbsolute(layers[activeLayer], 'top', 'top');
if(this.getScrollTop() === lastOffset) {
return;
}
document.body.style.overflow = 'hidden';
//Make sure to start animating at the last snap pos
this.setScrollTop(lastOffset);
if(info.direction === 'down') {
if(activeLayer + 1 < layers.length) {
activeLayer++;
var offset = this.relativeToAbsolute(layers[activeLayer], 'top', 'top');
//Move down slow with nice easing
this.animateTo(offset, animateDownSettings);
}
} else if(activeLayer > 0) {
activeLayer--;
var offset = this.relativeToAbsolute(layers[activeLayer], 'top', 'top');
//Move up very fast
this.animateTo(offset, animateUpSettings);
}
}
});
</script>
</body>
</html>