forked from zhiqiang21/WebComponent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzepto-slider.js
294 lines (250 loc) · 9.88 KB
/
zepto-slider.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
(function($) {
var SliderImgFunc = function(imgConter) {
var _this = this;
_this.touchObject = {};
_this.ImgSlider = imgConter;
_this.touchObject.prevDomNode = true;
_this.btnList = $('[node-type=btn-list]', imgConter);
_this.imgUlConter = $('[node-type=img-contenter]', imgConter);
_this.imgLiList = $('[node-type=img-list]', imgConter);
_this.setting = {
"autoAnimate": true, //是否自动播放
"btnShow": true, //是否显示图片切换按钮
"btnClick": true, //点击按钮是否切换图片
"touchMove": true,
"changeTime": 3000, //图片切换时间间隔
"animateType": 'ease-in', //动画类型
"animateTime": 500, //动画完成时间mm
"sweepTime": 400 //手指扫过切换图片的时间间隔 mm
};
$.extend(_this.setting, _this.getUserSetting());
_this.addLiValue();
//检测是否自动播放
if (_this.setting.autoAnimate) {
if (_this.imgLiList.length > 1) {
_this.startAnimate();
}
}
//检测是都显示按钮
if (_this.setting.btnShow) {
_this.showChangeBtn();
}
//按钮点击事件是否可用
if (_this.setting.btnClick) {
$(_this.ImgSlider).on('tap', 'li', function() {
_this.stopAnimate();
_this.trigBtnIndex = $(this).find('a').text();
_this.btnChangeImg();
setTimeout(function() {
_this.startAnimate();
}, 0);
});
}
//启用触摸事件
if (_this.setting.touchMove) {
$(_this.ImgSlider).on('touchstart', 'li', function() {
_this.stopAnimate();
_this.touchObject.currDomNode = this;
_this.touchStart.call(_this);
});
$(_this.ImgSlider).on('touchmove', 'li', function() {
_this.touchMove.call(_this);
});
$(_this.ImgSlider).on('touchend', 'li', function() {
_this.touchEnd.call(_this);
_this.startAnimate();
});
}
};
SliderImgFunc.prototype = {
//获取图片区域的宽度
slConWidth: function() {
return this.ImgSlider.width();
},
//是否显示图片上的按钮
showChangeBtn: function() {
if (this.imgLiList.length <= 1) {
// this.showChangeBtn();
return false;
} else {
this.btnList.css({
"display": 'list-item',
"list-style-type": "none"
});
}
},
//给包含图片的 li 添加一个数字索引
addLiValue: function() {
$.each(this.imgLiList, function(i, item) {
$(item).attr('value-type', i);
});
},
//开始播放动画,根据容器动态获取其中的图片的容器,使用 translate3d触发 gpu 渲染动画
startAnimate: function() {
var _this = this;
var moveConter = _this.imgUlConter;
_this.imgInterval = setInterval(function() {
moveConter.animate({
translate3d: '-' + _this.slConWidth() + 'px,0,0'
}, _this.setting.animateTime, _this.setting.animateType, delayAppend);
}, _this.setting.changeTime);
function delayAppend() {
setTimeout(function() {
_this.appendHtml();
}, 0);
}
},
appendHtml: function() {
var liList = $('[node-type=img-list]', this.ImgSlider);
var firstDom = liList.eq(0);
this.imgUlConter.append(firstDom);
this.changeBtnClass();
//初始化 ul 图片容器的值
this.imgUlConter.css('transform', 'translate3d(0,0,0)');
},
changeBtnClass: function(value) {
var liList = $('[node-type=img-list]', this.ImgSlider);
this.btnList.removeClass("current-btn");
this.btnList.eq(Number(liList.eq(0).attr('value-type'))).addClass('current-btn');
},
//按钮切换图片
btnChangeImg: function() {
var _this = this;
//触发的按钮
var trigBtnIndex = _this.trigBtnIndex - 1;
var imgLiList = _this.imgLiList;
var tempArray = [];
// 当前所在按钮的索引
var currBtnIndex = $('[class=current-btn]', _this.ImgSlider).text() - 1;
//重新获取 ul节点下图片节点的顺序
var newImgList = $('[node-type=img-list]', _this.ImgSlider);
if (currBtnIndex > trigBtnIndex) {
$.each(newImgList, function(index, el) {
var temp = Number($(el).attr('value-type'));
if (temp < currBtnIndex && temp >= trigBtnIndex) {
tempArray.push(el);
}
});
//将图片添加的当前图片之前,并且初始化容器的样式
_this.imgUlConter.prepend($(tempArray))
.css('transform', 'translate3d(0,0,0)');
} else {
$.each(imgLiList, function(i, item) {
if ($(item).attr('value-type') < trigBtnIndex) {
tempArray.push(item);
}
});
//将图片添加的当前图片之后,并且初始化容器的样式
_this.imgUlConter.append($(tempArray))
.css('transform', 'translate3d(0,0,0)');
}
$(imgLiList).removeClass('current-btn');
_this.changeBtnClass(trigBtnIndex);
},
//获取触摸的初始坐标
touchStart: function() {
var dateTime = new Date();
this.touchObject.startX = event.touches[0].pageX;
this.touchObject.startY = event.touches[0].pageY;
this.touchObject.startTime = dateTime.getTime();
},
touchMove: function() {
var ttb, tempMoveX, tempMoveY;
var newImgList = $('[node-type=img-list]', this.ImgSlider);
ttb = this.touchObject;
ttb.moveX = event.touches[0].pageX;
ttb.moveY = event.touches[0].pageY;
ttb.tempMoveX = ttb.moveX - ttb.startX;
ttb.tempMoveY = ttb.moveY - ttb.startY;
if (ttb.tempMoveX > 0) {
if (ttb.prevDomNode) {
this.imgUlConter.prepend(newImgList.eq(newImgList.length - 1))
// .css('transform', 'translate3d(0,0,0)');
.css('transform', 'translate3d(-' + this.slConWidth() + 'px,0,0)');
ttb.prevDomNode = false;
}
this.moveRight();
}
if (ttb.tempMoveX < 0) {
this.moveLeft();
}
},
touchEnd: function() {
var ttb, changeWidth, tempTime;
var dateTime = new Date();
ttb = this.touchObject;
changeWidth = this.slConWidth() / 2;
ttb.prevDomNode = true;
ttb.endTime = dateTime.getTime();
tempTime = ttb.endTime - ttb.startTime;
//调用 sweep判断是否切换动画或者是拖动切换
if (tempTime < this.setting.sweepTime) {
this.sweepFunc();
} else {
if (ttb.tempMoveX < 0) {
if (Math.abs(ttb.tempMoveX) > changeWidth) {
this.originLeft();
} else {
this.originRight();
}
} else {
if (Math.abs(ttb.tempMoveX) > changeWidth) {
this.originRight();
} else {
this.originLeft();
}
}
}
},
moveLeft: function() {
this.imgUlConter.css('transform', 'translate3d(' + this.touchObject.tempMoveX + 'px,0,0)');
},
moveRight: function() {
var moveTemp = this.slConWidth() - this.touchObject.tempMoveX;
this.imgUlConter.css('transform', 'translate3d(-' + moveTemp + 'px,0,0)');
},
//扫动的函数
sweepFunc:function(){
if (this.touchObject.tempMoveX < 0) {
this.originLeft();
} else {
this.originRight();
}
},
//因为滑动过半或者是不过半,返回的时间都应该至少是是做一次完整动画的一半时间
originRight: function() {
this.imgUlConter.animate({
'translate3d': '0,0,0'
}, this.setting.animateTime / 2, this.setting.animateType);
},
originLeft: function() {
var _this = this;
_this.imgUlConter.animate({
'translate3d': '-' + _this.slConWidth() + 'px,0,0'
}, _this.setting.animateTime / 2, _this.setting.animateType, function() {
_this.appendHtml();
});
},
stopAnimate: function() {
var _this = this;
clearTimeout(_this.imgInterval);
},
getUserSetting: function() {
var userSetting = this.ImgSlider.attr('user-setting');
if (userSetting && userSetting !== '') {
return $.parseJSON(userSetting);
} else {
return {};
}
},
destoryFunc: function() {
}
};
SliderImgFunc.init = function(imgConter) {
var _this = this;
imgConter.each(function() {
new _this($(this));
});
};
window.SliderImgFunc = SliderImgFunc;
})(Zepto);