-
Notifications
You must be signed in to change notification settings - Fork 2
/
slidepage.js
331 lines (280 loc) · 10.7 KB
/
slidepage.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
/**
* 通用全屏滑动切换动画组件
* @method slidepage.init
* @param {string}
* @return
* @example
slidepage.init({
container:'#demo1',
onSlide:function () {
console.info(this.index)
},
loadingId:'#loading',
loadingImgs:[
'http://imgcache.gtimg.cn/mediastyle/mobile/event/20141118_ten_jason/img/open_cover.jpg?max_age=2592000',
'http://imgcache.gtimg.cn/mediastyle/mobile/event/20141118_ten_jason/img/im_cover.jpg?max_age=2592000',
'http://imgcache.gtimg.cn/mediastyle/mobile/event/20141118_ten_jason/img/bg1.jpg?max_age=2592000',
'http://imgcache.gtimg.cn/mediastyle/mobile/event/20141118_ten_jason/img/bg2.jpg?max_age=2592000'
],
preLoadingImgs:[],
onLoading:function (complete,total) {
this.$('#loading div').style.width=complete/total*100+'%';
}
});
*
*
* 没有依赖
* @date 2014/11/3 星期一
* @author rowanyang
*
*/
;(function (win) {
function extend(a,b) {
for (var i in b) {
a[i]=b[i];
}
return a;
}
var slidepage = {
opts:{
speed:400,
isVertical:true,
loadingImgs:[],
preLoadingImgs:[],
onSlide:function () {},
onLoading:function () {}
},
wrap:null,
tplNum:0,
tpl:[],
index : 0,
$:function (o) {
return document.querySelector(o);
},
$$:function (o) {
return document.querySelectorAll(o);
},
init:function (opts) {
var self = this;
extend(this.opts,opts);
this.wrap=this.$(this.opts.container);
this.tpl=this.wrap.cloneNode(true).children;
this.tplNum=this.tpl.length; //总页数数据
this.touchInitPos = 0;//手指初始位置
this.startPos = 0;//移动开始的位置
this.totalDist = 0,//移动的总距离
this.deltaX1 = 0;//每次移动的正负
this.deltaX2 = 0;//每次移动的正负
this.displayWidth = document.documentElement.clientWidth; //图片区域最大宽度
this.displayHeight = document.documentElement.clientHeight; //图片区域最大高度
this.scrollDist=this.opts.isVertical?this.displayHeight:this.displayWidth;//滚动的区域尺寸
this.wrap.innerHTML='';
this.wrap.style.display="block"
if (this.opts.loadingImgs && this.opts.loadingImgs.length) {
this.loading();
}else {
this.pageInit();
}
this.$('body').addEventListener('touchstart',function (e) {
self.touchstart(e);
},false);
this.$('body').addEventListener('touchmove',function (e) {
self.touchmove(e);
},false);
this.$('body').addEventListener('touchend',function (e) {
self.touchend(e);
},false);
this.$('body').addEventListener('touchcancel',function (e) {
self.touchend(e);
},false);
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
},
pageInit:function () {
var self = this;
this.wrap.innerHTML='<div id="current" class="item '+this.tpl[0].className+'" style="-webkit-transform:translate3d(0,0,0)">'+this.tpl[0].innerHTML+'</div><div id="next" class="item item-1" style="-webkit-transform:translate3d('+this.getDist('100%')+')">'+this.tpl[1].innerHTML+'</div>';
setTimeout(function () {
self.$('#current').className+=' play'
});
},
touchstart : function (e) {
if(e.touches.length !== 1){return;}//如果大于1个手指,则不处理
this.touchInitPos = this.opts.isVertical ? e.touches[0].pageY:e.touches[0].pageX; // 每次move的触点位置
this.deltaX1 = this.touchInitPos;//touchstart的时候的原始位置
this.startPos = 0;
this.startPosPrev = -this.scrollDist;
this.startPosNext = this.scrollDist;
this.hasPrev = !!this.$('#prev');
this.hasNext = !!this.$('#next');
//手指滑动的时候禁用掉动画
if (this.hasNext) {
this.$('#next').style.cssText+='-webkit-transition-duration:0;'
}
this.$('#current').style.cssText+='-webkit-transition-duration:0;'
if (this.hasPrev) {
this.$('#prev').style.cssText+='-webkit-transition-duration:0;'
}
},
touchmove : function (e) {
if(e.touches.length !== 1){return;}
var currentX = this.opts.isVertical ? e.touches[0].pageY:e.touches[0].pageX;
this.deltaX2 = currentX - this.deltaX1;//记录当次移动的偏移量
this.totalDist = this.startPos + currentX - this.touchInitPos;
this.$('#current').style.WebkitTransform='translate3d('+this.getDist(this.totalDist+'px')+')';
this.startPos = this.totalDist;
//处理上一张和下一张
if (this.totalDist<0) {//露出下一张
if (this.hasNext) {
this.totalDist2 = this.startPosNext + currentX - this.touchInitPos;
this.$('#next').style.WebkitTransform='translate3d('+this.getDist(this.totalDist2+'px')+')';
this.startPosNext = this.totalDist2;
}
}else {//露出上一张
if (this.hasPrev) {
this.totalDist2 = this.startPosPrev + currentX - this.touchInitPos;
this.$('#prev').style.WebkitTransform='translate3d('+this.getDist(this.totalDist2+'px')+')';
this.startPosPrev = this.totalDist2;
}
}
this.touchInitPos = currentX;
},
touchend : function (e) {
if(this.deltaX2<-15){
this.next();
}else if(this.deltaX2 > 15){
this.prev();
}else{
this.itemReset();
}
this.deltaX2 = 0;
},
getDist:function (dist) {
return (this.opts.isVertical? '0,'+dist : dist+',0')+',0' ;
},
itemReset:function () {
this.$('#current').style.cssText+='-webkit-transition-duration:'+this.opts.speed+'ms;-webkit-transform:translate3d(0,0,0)';
if (this.$('#prev')) {
this.$('#prev').style.cssText+='-webkit-transition-duration:'+this.opts.speed+'ms;-webkit-transform:translate3d('+this.getDist('-'+this.scrollDist+'px')+')';
}
if (this.$('#next')) {
this.$('#next').style.cssText+='-webkit-transition-duration:'+this.opts.speed+'ms;-webkit-transform:translate3d('+this.getDist(this.scrollDist+'px')+')';
}
this.deltaX2 = 0;
},
prev:function () {
var self = this;
if (this.index > 0) {
this.index--;
}else {
this.itemReset();
return false;
}
var nextIndex = this.index+1 > this.tplNum-1 ? 0 : this.index+1;
this.$('#current').style.cssText+='-webkit-transition-duration:'+this.opts.speed+'ms;-webkit-transform:translate3d('+this.getDist(this.scrollDist+'px')+')';
if (this.$('#next')) {
this.wrap.removeChild(this.$('#next'));
}
this.$('#current').id='next';
this.$('#prev').style.cssText+='-webkit-transition-duration:'+this.opts.speed+'ms;-webkit-transform:translate3d(0,0,0)';
this.$('#prev').id='current';
setTimeout(function () {
self.$('#current').innerHTML=self.tpl[self.index].innerHTML;
if (self.$('.play')) {
self.$('.play').className=self.$('.play').className.replace(/\s*\bplay\b/g,'');
}
self.$('#current').className +=' play';
},400)
this.opts.onSlide.call(this);
var prevIndex = this.index-1;
if (prevIndex < 0) {
prevIndex = this.tplNum-1;
return false;
}
var addItem = document.createElement('div');
addItem.className='item item-'+prevIndex;
addItem.id='prev';
addItem.style.cssText+='-webkit-transition-duration:0ms;-webkit-transform:translate3d('+this.getDist('-'+this.scrollDist+'px')+')';
addItem.innerHTML=self.tpl[prevIndex].innerHTML;
this.wrap.insertBefore(addItem,this.$('#current'));
},
next:function () {
var self = this;
if (this.index < this.tplNum-1) {
this.index++;
}else {
this.itemReset();
return false;
}
var prevIndex = this.index===0 ? this.tplNum-1 : this.index-1;
this.$('#current').style.cssText+='-webkit-transition-duration:'+this.opts.speed+'ms;-webkit-transform:translate3d('+this.getDist('-'+this.scrollDist+'px')+')';
if (this.$('#prev')) {
this.wrap.removeChild(this.$('#prev'));
}
this.$('#current').id='prev';
this.$('#next').style.cssText+='-webkit-transition-duration:'+this.opts.speed+'ms;-webkit-transform:translate3d(0,0,0)';
this.$('#next').id='current';
window.t1=Date.now();
setTimeout(function () {
if (self.$('.play')) {
self.$('.play').className=self.$('.play').className.replace('play','');
}
self.$('#current').className +=' play';
self.opts.onSlide.call(self);
var nextIndex = self.index+1;
if (nextIndex >= self.tplNum) {
// $('#arr').style.display='none';
return false;
}
var addItem = document.createElement('div');
addItem.className='item item-'+nextIndex;
addItem.id='next';
addItem.style.cssText+='-webkit-transition-duration:0ms;-webkit-transform:translate3d('+self.getDist(self.scrollDist+'px')+')';
addItem.innerHTML=self.tpl[nextIndex].innerHTML;
self.wrap.appendChild(addItem);
},this.opts.speed)
log(Date.now()-t1)
},
loading:function () {
var self = this;
var imgurls=this.opts.loadingImgs;
var fallback=setTimeout(this.pageInit,15*1000);//超时时间 万一进度条卡那了 15秒后直接显示
this.wrap.appendChild(this.$(this.opts.loadingId));
var imgs=[], loaded=1;
var total=imgurls.length+1;
for (var i=0; i<imgurls.length; i++) {
imgs[i]=new Image();
imgs[i].src=imgurls[i];
imgs[i].onload=imgs[i].onerror=imgs[i].onabort=function (e) {
loaded++;
if (this.src === imgurls[0] && e.type === 'load') {
clearTimeout(fallback)
// ct_cover.init(this, function(cover) {
// ct_cover.remove();
// });
}
checkloaded();
}
}
self.opts.onLoading.call(self,1,total);
function checkloaded() {
self.opts.onLoading.call(self,loaded,total);
if (loaded==total) {
if (fallback) {
clearTimeout(fallback)
}
self.pageInit();
imgs=null;
if (self.opts.preLoadingImgs && self.opts.preLoadingImgs.length) {
self.preloading();
}
}
}
},
preloading:function () {
}
}
if (typeof module == 'object') {
module.exports=slidepage;
}else {
win.slidepage=slidepage;
}
})(window);