/
swipe2.js
executable file
·417 lines (375 loc) · 15.5 KB
/
swipe2.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* 并没有做好模块化,其实应该只提供一个骨架,也就是上下/左右滑动的监听
* 1.初始化
获取父元素/子元素,而后初始化子元素的位置,【这方面的工作,也可以让css3来做】
* 2.事件
touchstart
touchmove
touchend
* 3.接口,对外提供的接口有三
其实也就是滑动前,滑动中,滑动结束
每一次滑动结束后,需要对一些dom进行一些重置工作,虽然说这些充值工作,也可以在touchstart的时候进行
但那样会影响用户体现
4.参数,有时候需要另外一些参数
初始化的时候,需要外界传递一个currentPage当前页面,也可以通过参数来传递进来的
*/
var Lunbo = function(arg) { //以对象形式传递参数
var idname = arg.idname,
top = arg.top,
autoPlay = !!arg.autoPlay,
loop = !!arg.loop,
dianNav = !!arg.dianNav,
callback = arg.callback,
buttPrev = arg.buttPrev,
buttNext = arg.buttNext,
keyEvent = arg.keyEvent,
time = arg.time ? arg.time : '0.6s' ,
currentPage = isNaN(parseInt(arg.currentPage,10))?0:arg.currentPage ;
var isPhone = !!navigator.userAgent.toLowerCase().match(/android|phone|pad/g);
var $id = document.querySelector('#' + idname);
var $item = [].slice.call($id.firstElementChild.children);
var len = $item.length ;
if(len<=1) return ;
var $dianItem;
var idWidth = $id.offsetWidth,
idHeight = $id.offsetHeight;
var topMin = -idHeight, topCurrent = 0, topMax = idHeight, leftMin = 0, leftMax = 0, leftCurrent = 0, toLeft = false, timeIn ;
if (top == 'left') //左右滑动
{
toLeft = true, topMin = 0, topMax = 0, topCureent = 0, leftMin = -idWidth, leftMax = idWidth, leftCurrent = 0;
}
var xx, XX, currentDom, prevDom, nextDom, swipeX, swipeY, cha, chaCache;
var mouseStart, mouseMove, mouseEnd ;
var swipeable = true ;
var MINSDISTANCE = 50 ;
var TRANSTION = 'all '+time+' cubic-bezier(0.455, 0.03, 0.515, 0.955)' ;
var touchState = 'touchend';
var ISRAF = !!window.requestAnimationFrame ;
mouseStart = isPhone ? 'touchstart' : 'mousedown';
mouseMove = isPhone ? 'touchmove' : 'mousemove';
mouseEnd = isPhone ? 'touchend' : 'mouseup';
$item.forEach(function(ele, index) {
if (index != currentPage) ele.style.webkitTransform = 'translate3d(' + leftMax + 'px,' + topMax + 'px,0)';
});
if (dianNav) dianInit();
if (autoPlay) start();
if(buttNext || buttPrev) buttEvent();
var stateControl = {
init:function(){
/*----------初始化一下----------*/
currentPage = setPage(currentPage);
currentDom = $item[currentPage];
prevDom = $item[currentPage - 1] ? $item[currentPage - 1] : $item[len - 1];
nextDom = $item[currentPage + 1] ? $item[currentPage + 1] : $item[0];
resetStyle();
},
end:function(){
swipeY = false ;
swipeX = false ;
swipeable = false ;
currentDom.style.webkitTransition = TRANSTION ;
prevDom.style.webkitTransition = TRANSTION ;
nextDom.style.webkitTransition = TRANSTION ;
},
start:function(event){
if ( !swipeable ) {
swipeX = false;
swipeY = false;
return
}
cha = 0;
if (autoPlay) stop();
swipeY = true;
swipeX = true;
},
removeTransition:function(){
currentDom.style.webkitTransition = 'none';
prevDom.style.webkitTransition = 'none';
nextDom.style.webkitTransition = 'none';
},
setTransform:function(dom,translateX,translateY,scaleX,scaleY){
scaleX = scaleX || 1 ;
scaleY = scaleY || 1 ;
dom.style.webkitTransform = toLeft ? 'translate3d('+translateX+'px,'+translateY+'px,0)' : 'translate3d('+translateX+'px,'+translateY+'px,0) scale3d('+scaleX+','+scaleY+',1)' ;
},
callback:function(){
if (autoPlay) start();
if (dianNav) dianMove();
if (callback) callback(currentPage);
setDom();
swipeable = true ;
stateControl.animationFrame.map(window.cancelAnimationFrame)
stateControl.animationFrame.length = 0 ;
},
animationFrame:[],
moveAnimationFrame:[],
}
stateControl.init();
stateControl.callback();
// 这个地方可以再整合一下,towhere和滑动的本质应该是一样的
// 上一页、下一页也只是towhere的参数
var targetTouch , event ;
$id.addEventListener(mouseStart, function(event) {
touchState = 'touchstart' ;
stateControl.start(event);
XX = xx = isPhone ? event.targetTouches[0].screenX : event.pageX;
YY = yy = isPhone ? event.targetTouches[0].screenY : event.pageY;
});
// 这里有个问题,如果在PC端,一直移动鼠标会阻塞其他程序执行
// 当然在移动端,不听滑动屏幕,也会有这个问题
$id.addEventListener(mouseMove, touchMove);
$id.addEventListener(mouseEnd, touchEnd);
// 按键监听
if (!isPhone) {
window.addEventListener('keyup', function(event) { // 按键监听
if (!keyEvent) return
var index;
if (event.keyCode == 40 || event.keyCode == 39) index = setPage((currentPage+1),true), toWhere(index, 'next');
else if (event.keyCode == 38 || event.keyCode == 37) index = setPage((currentPage-1),true), toWhere(index, 'prev');
});/*
window.addEventListener('wheel',function(event){
console.log('鼠标滚动',event)
})*/
}
/*---------------------------------------问题在这里------------------------------------------------------------*/
$item.forEach(function(ele) {
ele.addEventListener('webkitTransitionEnd', function() {
chaCache++ ;
if (chaCache === 2) {
ISRAF ? stateControl.animationFrame.push(window.requestAnimationFrame(stateControl.callback) ) : stateControl.callback() ;
}
});
});
function touchMove(event){
touchState = 'touchmove';
if(!swipeable || touchState!='touchmove') return
XX = isPhone ? event.targetTouches[0].screenX : event.pageX;
YY = isPhone ? event.targetTouches[0].screenY : event.pageY;
if (swipeY && (!swipeX || Math.abs(XX - xx) - Math.abs(YY - yy) < 0)) {
swipeX = false;
if (!toLeft) {
event.stopPropagation();
event.preventDefault();
}
}
if (swipeX && (!swipeY || Math.abs(XX - xx) - Math.abs(YY - yy) > 0)) {
swipeY = false;
if (toLeft) {
event.stopPropagation();
event.preventDefault();
}
}
if(ISRAF){
window.requestAnimationFrame(touchMoveRender.bind(this,event));
}
else{
touchMoveRender(event)
}
}
// move render dom
function touchMoveRender(event) { // 滑动中
if (swipeY && (!swipeX || Math.abs(XX - xx) - Math.abs(YY - yy) < 0)) {
if (!toLeft) {
cha = YY - yy;
if (cha >= 0) {
if (len > 2) stateControl.setTransform(nextDom,leftMax,topMax); // 避免因为滑动过快引起的bug
if (!loop && currentPage == 0) return cha = 0;
currentDom.style.webkitTransformOrigin = 'center 125%';
stateControl.setTransform(currentDom,0,0,(1 - cha / topMax / 4),(1 - cha / topMax / 4));
stateControl.setTransform(prevDom,0,(cha + topMin));
} else {
if (len > 2) stateControl.setTransform(prevDom,0,topMax); // 避免因为滑动过快引起的bug
if (!loop && currentPage == len - 1) return cha = 0;
currentDom.style.webkitTransformOrigin = 'center -25%';
stateControl.setTransform(currentDom,0,0,(1 + cha / topMax / 4),(1 + cha / topMax / 4));
stateControl.setTransform(nextDom,0,(cha + topMax));
}
}
}
if (swipeX && (!swipeY || Math.abs(XX - xx) - Math.abs(YY - yy) > 0)) {
if (toLeft) {
cha = XX - xx;
if (cha >= 0) {
if (len > 2) stateControl.setTransform(nextDom,leftMax,topMax); // 避免因为滑动过快引起的bug
if (!loop && currentPage == 0) return cha = 0;
stateControl.setTransform(currentDom,cha,0);
stateControl.setTransform(prevDom,(leftMin + cha),0);
} else {
if (len > 2) stateControl.setTransform(prevDom,leftMax,topMax); // 避免因为滑动过快引起的bug
if (!loop && currentPage == len - 1) return cha = 0;
stateControl.setTransform(currentDom,cha,0);
stateControl.setTransform(nextDom,(leftMax + cha),0);
}
}
}
stateControl.moveAnimationFrame.length = 0 ;
}
function touchEnd(event) { // 滑动结束
touchState = 'touchend';
if (cha == 0 || !swipeable) {
swipeY = false;
swipeX = false;
return;
}
if (!toLeft && swipeY) // 上下滑动
{
var scale = cha > 0 ? (1 - cha / topMax / 4 * 1.4) : (1 + cha / topMax / 4 * 1.4);
scale.toFixed(1);
stateControl.end();
if (cha > 0) {
if (cha > MINSDISTANCE) {
stateControl.setTransform(currentDom,0,0,scale,scale);
stateControl.setTransform(prevDom,0,0);
setPage( (--currentPage) );
} else {
stateControl.setTransform(currentDom,0,0);
stateControl.setTransform(prevDom,0,topMin);
}
} else {
if (cha < -MINSDISTANCE) {
stateControl.setTransform(currentDom,0,0,scale,scale);
stateControl.setTransform(nextDom,0,0);
setPage( (++currentPage) );
} else {
stateControl.setTransform(currentDom,0,0);
stateControl.setTransform(nextDom,0,topMax);
}
}
}
if (toLeft && swipeX) // 左右滑动
{
stateControl.end();
if (cha > 0) {
if (cha > MINSDISTANCE) {
stateControl.setTransform(currentDom,leftMax,0 );
stateControl.setTransform(prevDom,0,0);
setPage( (--currentPage) );
} else {
stateControl.setTransform(currentDom,0,0 );
stateControl.setTransform(prevDom,leftMin,0);
}
} else {
if (cha < -MINSDISTANCE) {
stateControl.setTransform(currentDom,leftMin,0 );
stateControl.setTransform(nextDom,0,0);
setPage( (++currentPage) );
} else {
stateControl.setTransform(currentDom,0,0 );
stateControl.setTransform(nextDom,leftMax,0);
}
}
}
// 重置chaCache
chaCache = 0;
}
// 通过设置z-index来解决层级问题
function setDom() { // 初始化工作,不放在touchstart时执行,而是在滑动结束的时候执行,这样体验会更好些
stateControl.removeTransition();
var dom ;
if (cha < MINSDISTANCE && cha > -MINSDISTANCE) return;
else if (cha >= MINSDISTANCE) dom = nextDom;
else if (cha <= -MINSDISTANCE) dom = prevDom;
if (toLeft) dom && (dom.style.visibility = 'hidden');
else dom && (dom.style.cssText += ';visibility:hidden; z-index:auto;');
prevDom = $item[currentPage - 1] ? $item[currentPage - 1] : $item[len - 1]; // 重置 上一个/当前/下一个
nextDom = $item[currentPage + 1] ? $item[currentPage + 1] : $item[0];
currentDom = $item[currentPage];
resetStyle();
};
function resetStyle() {
var str1 = ';-webkit-transform:translate3d(0,0,0); visibility:visible;' ,
str2 = ';-webkit-transform:translate3d(' + leftMin + 'px,' + topMin + 'px,0); visibility: visible; ' ,
str3 = ';-webkit-transform:translate3d(' + leftMax + 'px,' + topMax + 'px,0); visibility:visible; ';
currentDom.style.cssText += str1+(toLeft?'':'z-index:1;');
prevDom.style.cssText += str2+(toLeft?'':'z-index:200;');
nextDom.style.cssText += str3+(toLeft?'':'z-index:200;');
}
function setPage(page,option) { //设置page
if (page > len - 1) {
page = 0;
if (!loop) page = len - 1;
} else if (page < 0) {
page = len - 1;
if (!loop) page = 0;
}
if(!option) currentPage = page;
return page ;
}
/*
* 主要是给左右点击事件使用
* index 下一个页面的 页码?
* dir 向左还是向右滑动 next/prev
*/
function toWhere(index, dir) {
// console.log(111,swipeable)
if(!swipeable) return
index = setPage(index,true);
if (index === currentPage) return
stateControl.start();
cha = (dir=="next") ? -100 : 100 ;
stateControl.removeTransition();
currentDom = $item[currentPage];
cha<0 ? (nextDom = $item[index]) : (prevDom = $item[index] ) ;
stateControl.removeTransition();
resetStyle();
currentDom.offsetWidth ;
if(!toLeft){
if(cha>0) currentDom.style.webkitTransformOrigin = 'center 125%';
else currentDom.style.webkitTransformOrigin = 'center -25%';
}
touchEnd();
currentPage = index ;
}
this.toWhere = toWhere;
// 点击事件
function buttEvent() {
if (!!buttNext) {
var click = isPhone ? 'touchend' : 'click';
$id.querySelector(buttPrev).addEventListener(click, function(event) {
var index = currentPage-1;
toWhere(index, 'prev');
});
}
if (!!buttPrev) {
var click = isPhone ? 'touchend' : 'click';
$id.querySelector(buttNext).addEventListener(click, function() {
var index = currentPage+1;
toWhere(index, 'next');
});
}
}
function dianMove() { //下面小点的运动
$dianItem.forEach(function(ele, index) {
if (index == currentPage) ele.classList.add('dian-item-current');
else ele.classList.remove('dian-item-current');
});
}
function appendDian() { //添加点
var dian = [];
for (var i = 0; i < len; i++) {
dian.push('<span class="dian-item"></span>');
}
var innerHTML = dian.join('');
var fg = document.createDocumentFragment();
var div = document.createElement('div');
div.className = 'dian';
fg.appendChild(div);
var fgDian = fg.querySelector('div');
fgDian.innerHTML = innerHTML;
$id.appendChild(fgDian);
}
function dianInit() {
appendDian();
$dianItem = [].slice.call(document.querySelector('#' + idname).querySelectorAll('.dian-item'));
dianMove();
}
function start() { //自动轮播
timeIn = setInterval(move, 4000);
}
function stop() { //清除自动轮播
clearInterval(timeIn);
}
function move() {
toWhere(currentPage + 1, 'next')
}
}