Skip to content

Commit d48112b

Browse files
committed
edit file
1 parent 68bb2e8 commit d48112b

16 files changed

+296
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

MySlider/zepto-slider.js

+295
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
;
2+
(function($) {
3+
var SliderImgFunc = function(imgConter) {
4+
var _this = this;
5+
6+
_this.touchObject = {};
7+
_this.ImgSlider = imgConter;
8+
_this.touchObject.prevDomNode = true;
9+
10+
11+
_this.btnList = $('[node-type=btn-list]', imgConter);
12+
_this.imgUlConter = $('[node-type=img-contenter]', imgConter);
13+
_this.imgLiList = $('[node-type=img-list]', imgConter);
14+
15+
16+
_this.setting = {
17+
"autoAnimate": true, //是否自动播放
18+
"btnShow": true, //是否显示图片切换按钮
19+
"btnClick": true, //点击按钮是否切换图片
20+
"touchMove": true,
21+
"changeTime": 3000, //图片切换时间间隔
22+
"animateType": 'ease-in', //动画类型
23+
"animateTime": 500, //动画完成时间mm
24+
"sweepTime": 400 //手指扫过切换图片的时间间隔 mm
25+
};
26+
27+
$.extend(_this.setting, _this.getUserSetting());
28+
29+
30+
_this.addLiValue();
31+
32+
//检测是否自动播放
33+
if (_this.setting.autoAnimate) {
34+
if (_this.imgLiList.length > 1) {
35+
_this.startAnimate();
36+
}
37+
}
38+
39+
//检测是都显示按钮
40+
if (_this.setting.btnShow) {
41+
_this.showChangeBtn();
42+
}
43+
44+
//按钮点击事件是否可用
45+
if (_this.setting.btnClick) {
46+
$(_this.ImgSlider).on('tap', 'li', function() {
47+
_this.stopAnimate();
48+
_this.trigBtnIndex = $(this).find('a').text();
49+
_this.btnChangeImg();
50+
setTimeout(function() {
51+
_this.startAnimate();
52+
}, 0);
53+
});
54+
}
55+
56+
//启用触摸事件
57+
if (_this.setting.touchMove) {
58+
$(_this.ImgSlider).on('touchstart', 'li', function() {
59+
_this.stopAnimate();
60+
_this.touchObject.currDomNode = this;
61+
_this.touchStart.call(_this);
62+
});
63+
$(_this.ImgSlider).on('touchmove', 'li', function() {
64+
_this.touchMove.call(_this);
65+
});
66+
$(_this.ImgSlider).on('touchend', 'li', function() {
67+
_this.touchEnd.call(_this);
68+
_this.startAnimate();
69+
});
70+
}
71+
72+
};
73+
74+
SliderImgFunc.prototype = {
75+
//获取图片区域的宽度
76+
slConWidth: function() {
77+
return this.ImgSlider.width();
78+
},
79+
//是否显示图片上的按钮
80+
showChangeBtn: function() {
81+
if (this.imgLiList.length <= 1) {
82+
// this.showChangeBtn();
83+
return false;
84+
} else {
85+
this.btnList.css({
86+
"display": 'list-item',
87+
"list-style-type": "none"
88+
});
89+
}
90+
},
91+
//给包含图片的 li 添加一个数字索引
92+
addLiValue: function() {
93+
$.each(this.imgLiList, function(i, item) {
94+
$(item).attr('value-type', i);
95+
});
96+
},
97+
//开始播放动画,根据容器动态获取其中的图片的容器,使用 translate3d触发 gpu 渲染动画
98+
startAnimate: function() {
99+
var _this = this;
100+
var moveConter = _this.imgUlConter;
101+
102+
_this.imgInterval = setInterval(function() {
103+
moveConter.animate({
104+
translate3d: '-' + _this.slConWidth() + 'px,0,0'
105+
}, _this.setting.animateTime, _this.setting.animateType, delayAppend);
106+
}, _this.setting.changeTime);
107+
108+
function delayAppend() {
109+
setTimeout(function() {
110+
_this.appendHtml();
111+
}, 0);
112+
}
113+
},
114+
appendHtml: function() {
115+
var liList = $('[node-type=img-list]', this.ImgSlider);
116+
var firstDom = liList.eq(0);
117+
118+
this.imgUlConter.append(firstDom);
119+
this.changeBtnClass();
120+
121+
//初始化 ul 图片容器的值
122+
this.imgUlConter.css('transform', 'translate3d(0,0,0)');
123+
124+
},
125+
changeBtnClass: function(value) {
126+
var liList = $('[node-type=img-list]', this.ImgSlider);
127+
this.btnList.removeClass("current-btn");
128+
this.btnList.eq(Number(liList.eq(0).attr('value-type'))).addClass('current-btn');
129+
},
130+
//按钮切换图片
131+
btnChangeImg: function() {
132+
var _this = this;
133+
134+
//触发的按钮
135+
var trigBtnIndex = _this.trigBtnIndex - 1;
136+
var imgLiList = _this.imgLiList;
137+
var tempArray = [];
138+
139+
// 当前所在按钮的索引
140+
var currBtnIndex = $('[class=current-btn]', _this.ImgSlider).text() - 1;
141+
142+
//重新获取 ul节点下图片节点的顺序
143+
var newImgList = $('[node-type=img-list]', _this.ImgSlider);
144+
145+
146+
if (currBtnIndex > trigBtnIndex) {
147+
$.each(newImgList, function(index, el) {
148+
var temp = Number($(el).attr('value-type'));
149+
if (temp < currBtnIndex && temp >= trigBtnIndex) {
150+
tempArray.push(el);
151+
}
152+
});
153+
//将图片添加的当前图片之前,并且初始化容器的样式
154+
_this.imgUlConter.prepend($(tempArray))
155+
.css('transform', 'translate3d(0,0,0)');
156+
} else {
157+
$.each(imgLiList, function(i, item) {
158+
if ($(item).attr('value-type') < trigBtnIndex) {
159+
tempArray.push(item);
160+
}
161+
});
162+
163+
//将图片添加的当前图片之后,并且初始化容器的样式
164+
_this.imgUlConter.append($(tempArray))
165+
.css('transform', 'translate3d(0,0,0)');
166+
}
167+
168+
$(imgLiList).removeClass('current-btn');
169+
_this.changeBtnClass(trigBtnIndex);
170+
},
171+
172+
//获取触摸的初始坐标
173+
touchStart: function() {
174+
var dateTime = new Date();
175+
176+
this.touchObject.startX = event.touches[0].pageX;
177+
this.touchObject.startY = event.touches[0].pageY;
178+
this.touchObject.startTime = dateTime.getTime();
179+
},
180+
181+
touchMove: function() {
182+
var ttb, tempMoveX, tempMoveY;
183+
var newImgList = $('[node-type=img-list]', this.ImgSlider);
184+
185+
ttb = this.touchObject;
186+
ttb.moveX = event.touches[0].pageX;
187+
ttb.moveY = event.touches[0].pageY;
188+
189+
ttb.tempMoveX = ttb.moveX - ttb.startX;
190+
ttb.tempMoveY = ttb.moveY - ttb.startY;
191+
192+
if (ttb.tempMoveX > 0) {
193+
if (ttb.prevDomNode) {
194+
this.imgUlConter.prepend(newImgList.eq(newImgList.length - 1))
195+
// .css('transform', 'translate3d(0,0,0)');
196+
.css('transform', 'translate3d(-' + this.slConWidth() + 'px,0,0)');
197+
ttb.prevDomNode = false;
198+
}
199+
this.moveRight();
200+
}
201+
202+
if (ttb.tempMoveX < 0) {
203+
this.moveLeft();
204+
}
205+
206+
},
207+
208+
touchEnd: function() {
209+
var ttb, changeWidth, tempTime;
210+
var dateTime = new Date();
211+
212+
ttb = this.touchObject;
213+
changeWidth = this.slConWidth() / 2;
214+
ttb.prevDomNode = true;
215+
ttb.endTime = dateTime.getTime();
216+
tempTime = ttb.endTime - ttb.startTime;
217+
218+
//调用 sweep判断是否切换动画或者是拖动切换
219+
if (tempTime < this.setting.sweepTime) {
220+
this.sweepFunc();
221+
} else {
222+
if (ttb.tempMoveX < 0) {
223+
if (Math.abs(ttb.tempMoveX) > changeWidth) {
224+
this.originLeft();
225+
} else {
226+
this.originRight();
227+
}
228+
} else {
229+
if (Math.abs(ttb.tempMoveX) > changeWidth) {
230+
this.originRight();
231+
} else {
232+
this.originLeft();
233+
}
234+
}
235+
}
236+
},
237+
238+
moveLeft: function() {
239+
this.imgUlConter.css('transform', 'translate3d(' + this.touchObject.tempMoveX + 'px,0,0)');
240+
},
241+
242+
moveRight: function() {
243+
var moveTemp = this.slConWidth() - this.touchObject.tempMoveX;
244+
this.imgUlConter.css('transform', 'translate3d(-' + moveTemp + 'px,0,0)');
245+
},
246+
247+
//扫动的函数
248+
sweepFunc:function(){
249+
if (this.touchObject.tempMoveX < 0) {
250+
this.originLeft();
251+
} else {
252+
this.originRight();
253+
}
254+
},
255+
256+
//因为滑动过半或者是不过半,返回的时间都应该至少是是做一次完整动画的一半时间
257+
originRight: function() {
258+
this.imgUlConter.animate({
259+
'translate3d': '0,0,0'
260+
}, this.setting.animateTime / 2, this.setting.animateType);
261+
},
262+
originLeft: function() {
263+
var _this = this;
264+
_this.imgUlConter.animate({
265+
'translate3d': '-' + _this.slConWidth() + 'px,0,0'
266+
}, _this.setting.animateTime / 2, _this.setting.animateType, function() {
267+
_this.appendHtml();
268+
});
269+
},
270+
stopAnimate: function() {
271+
var _this = this;
272+
clearTimeout(_this.imgInterval);
273+
},
274+
getUserSetting: function() {
275+
var userSetting = this.ImgSlider.attr('user-setting');
276+
if (userSetting && userSetting !== '') {
277+
return $.parseJSON(userSetting);
278+
} else {
279+
return {};
280+
}
281+
},
282+
destoryFunc: function() {
283+
284+
}
285+
};
286+
287+
SliderImgFunc.init = function(imgConter) {
288+
var _this = this;
289+
imgConter.each(function() {
290+
new _this($(this));
291+
});
292+
};
293+
294+
window.SliderImgFunc = SliderImgFunc;
295+
})(Zepto);

MySlider/zepto-slider.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)