-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
205 lines (189 loc) · 6.89 KB
/
demo.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<style>
* {
padding: 0;
margin: 0;
}
html, body {
height: 100%;
}
body {
overflow: hidden;
}
.pages {
height: 100%;
}
.page {
position: relative;
height: 100%;
overflow: hidden;
}
.pages .page:nth-child(1) {
background: red;
}
.pages .page:nth-child(2) {
background: green;
}
.pages .page:nth-child(3) {
background: blue;
}
</style>
</head>
<body>
<div class="pages">
<div class="page active"></div>
<div class="page"></div>
<div class="page"></div>
</div>
<script>
function $(selector, parentNode) {
parentNode = parentNode || document;
return parentNode.querySelector(selector);
}
function $$(selector, parentNode) {
parentNode = parentNode || document;
return parentNode.querySelectorAll(selector);
}
function pageScroll(options) {
var opts = {
container : $(options.container),
direction : options.direction || 'vertical',
effect : options.effect || '',
start : options.start || function(){},
end : options.end || function(){},
scrollDuration : 300,
springbackDuration: 200
};
var touch = null;
var child = opts.container.children;
var maxIndex = child.length - 1;
var onceDistance = opts.direction === 'vertical' ? document.documentElement.clientHeight: document.documentElement.clientWidth;
var current = {
index: 0,
position: 0
};
var evts = {
'touchstart': function (e) {
e.preventDefault();
e.stopPropagation();
//touch被销毁或第一次触摸才响应,否则页面还在动画中不响应
if (!touch) {
initTouch(e);
opts.start.call(child[current.index], current.index);
}
},
'touchmove': function (e) {
e.preventDefault();
e.stopPropagation();
if (touch) {
touch.distance = e.touches[0].pageY - touch.startY;
touch.direction = touch.distance < 0 ? 'forward' : 'backward';
if (touch.direction === 'backward' && current.index === 0 || touch.direction === 'forward' && current.index === maxIndex) {
initTouch(e);
} else {
setCssText(touch.distance + current.position);
}
}
},
'touchend': function (e) {
e.preventDefault();
e.stopPropagation();
if (touch && touch.distance) {
touch.costTime = Date.now() - touch.startTime;
upliftHandler();
}
touch = null;
}
};
//防止在滑动过程中失去焦点导致停止
evts.touchcancel = evts.touchend;
bindEvents(evts);
//初始化touch信息
function initTouch(e) {
touch = {
distance: 0,
startX: e.touches[0].pageX,
startY: e.touches[0].pageY,
startTime: Date.now()
};
}
//滑动后抬起的处理
function upliftHandler() {
if (Math.abs(touch.distance) > onceDistance/6) {
scroll();
} else {
springback();
}
}
//翻页
function scroll() {
var oldIndex = current.index;
if (touch.direction === 'forward') {
current.index += 1;
} else {
current.index -= 1;
}
current.position = -current.index * onceDistance;
setCssText(current.position, opts.scrollDuration);
setTimeout(function () {
opts.end.apply(child[current.index], [current.index, oldIndex, child[oldIndex]]);
}, opts.scrollDuration);
}
//不满足翻页条件页面回弹
function springback() {
setCssText(current.position, opts.springbackDuration);
}
function setCssText(position, duration) {
var prefix = ['webkit', 'moz', 'o', 'ms'];
var cssText = [];
var addText = function (text) {
for (var i = 0, len = prefix.length; i < len; i++) {
cssText.push('-' + prefix[i] + '-' + text);
}
cssText.push(text);
};
if (opts.direction === 'vertical') {
addText('transform:translate3d(0,' + position +'px,0)');
} else {
addText('transform:translate3d(' + position +'px,0,0)');
}
duration && addText('transition:' + ['transform', opts.effect, duration + 'ms'].join(' '));
opts.container.style.cssText = cssText.join(';');
}
function bindEvents(events) {
//TODO:safari出现滚动条
$('html').addEventListener('touchstart', function(){});
//页面调整自适应
window.addEventListener('resize', function () {
onceDistance = opts.direction === 'vertical' ? document.documentElement.clientHeight: document.documentElement.clientWidth;
current.position = -current.index * onceDistance;
setCssText(current.position);
});
for (var name in events) {
if (events.hasOwnProperty(name)) {
opts.container.addEventListener(name, events[name], false);
}
}
}
}
pageScroll({
container: '.pages',
direction: 'vertical',
effect: 'ease',
start: function (index) {
console.log('start', this, index);
},
end: function (index, prevIndex, prevPage) {
console.log('end', this, index, prevIndex, prevPage);
this.classList.add('active');
prevPage.classList.remove('active');
}
});
</script>
</body>
</html>