-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (169 loc) · 7.24 KB
/
index.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
var switchSwiper = function (options) {
// 获取元素
this.el = options.el || $('.swiperSwiper');
// 获取需要操作的子元素
this.sonEvent = this.el.children('.swiper-box').children('.swiper-alone');
// 计算子元素的数量
this.sonEventNum = this.sonEvent.length;
// 是否开启自动滚播
this.autoPlay = options.autoPlay && true;
// 自动滚播的播放间隔时间
this.autoPlayTime = options.autoPlayTime || 2000;
// 图片切换的速度
this.autoPlaySpeed = options.autoPlaySpeed || 500;
// 后续图片缩进尺寸
this.indentation = options.indentation || 30;
// 是否显示导航点
this.dots = false;
// 后续图片缩放比例,精确到小数点后两位
this.zoom = function() {
return (100 - ((options.zoom || 0.9) * 100)) / 100;
}
var clearAutoPlay;
// 初始的css设置
this.settingCss = function() {
// 根据排列设置z-index和缩进
for(let i = 1; i < (this.sonEventNum + 1); i++){
// 计算并设置缩放和缩进
var scaleNum = 1 - ((i - 1) * this.zoom()),
translateXNum = (i - 1) * this.indentation;
this.sonEvent.eq(this.sonEventNum - i).css({'z-index': i});
this.sonEvent.eq(i - 1).css({'transform': 'scale('+scaleNum+') translateX('+translateXNum+'px)'});
}
// 设置动画的执行时间
this.sonEvent.css('transition', 'all '+ this.autoPlaySpeed +'ms ease');
}
// 监听动画结束 - 辅助性函数
this.animateEnd = function (event, fun) {
function transitionSonEnd() {
fun();
event.off('transitionend',transitionSonEnd);
}
event.on('transitionend',transitionSonEnd);
}
// 根据排列计算位置 - 辅助性函数
this.countScale = function(num) {
return 1 - (num * this.zoom());
}
this.countTranslateX = function(num) {
return num * this.indentation;
}
// 获取手指拖动切换的阈值
this.moveValue = function() {
return (parseInt(this.sonEvent.css('width')) / 2);
}
// 向左切换
this.theLeft = function() {
// 初始更新一下DOM列表,然后把第一个元素做出场动画
this.sonEventNew = this.el.children('.swiper-box').children('.swiper-alone');
this.sonEventNew.eq(0).css({'transform': 'translateX(-200px)','opacity': 0});
// 然后做剩下元素的动画
for(let i = 0;i < this.sonEventNum;i ++) {
var scaleNum = 1 - (i * this.zoom()),
translateXNum = i * this.indentation;
this.sonEventNew.eq(i).css({'z-index': (this.sonEventNum - i + 1)});
this.sonEventNew.eq(i + 1).css({'transform': 'scale('+scaleNum+') translateX('+translateXNum+'px)'});
}
// 计算出动画后最后一个元素应该在的位置
var lastScaleNum = 1 - ((this.sonEventNum - 1) * this.zoom()),
lastTranslateXNum = (this.sonEventNum - 1) * this.indentation;
// 然后再克隆第一个元素,将元素置于最后
var lastEvent = this.sonEventNew.eq(0).clone();
lastEvent.appendTo('.swiper-box');
lastEvent.css({'z-index': '1','transform': 'scale('+lastScaleNum+') translateX('+lastTranslateXNum+'px)','opacity': '1'});
// 最后在动画结束之后删除第一个元素
var than = this;
setTimeout(function() {
than.sonEventNew.eq(0).remove();
}, this.autoPlaySpeed);
}
// 向右切换
this.theRight = function() {
// 初始更新一下DOM列表,然后克隆最后出最后一个子元素
this.sonEventNew = this.el.children('.swiper-box').children('.swiper-alone');
var lastEvent = this.sonEventNew.eq(this.sonEventNum - 1);
this.lastEventClone = lastEvent.clone();
// 监听最后一个元素消失后就删除
lastEvent.css('opacity', '0');
this.animateEnd(lastEvent, function() {
lastEvent.remove();
});
// 将其他的元素向后移动一位
for(let i = 0;i < (this.sonEventNum);i ++) {
// 计算位置
var scaleNum = 1 - ((i + 1) * this.zoom()),
translateXNum = (i + 1) * this.indentation;
this.sonEventNew.eq(i).css({'transform': 'scale('+scaleNum+') translateX('+translateXNum+'px)', 'z-index': (this.sonEventNum - i - 1)});
}
// 将初始时克隆的元素放到首位并出现
this.lastEventClone.prependTo('.swiper-box');
this.lastEventClone.css({'transition': 'all '+ this.autoPlaySpeed +'ms ease', 'transform': 'translateX(-200px)','opacity': 0, 'z-index': this.sonEventNum});
var than = this;
setTimeout(function(){
than.lastEventClone.css({
'transform': 'translateX(0px)',
'opacity': 1});
},100);
}
// 手动切换
this.manualSelect = function() {
var touchMain = document.querySelector('.swiper-box'),
moveEvent,startX,moveX,endX,moveDistance,
than = this;
function touchs(e) {
var touch = e.touches[0];
startX = Math.floor(touch.pageX);
// console.log('touchstart = ' + startX);
// 鼠标按住的时候获取当前的第一个元素,并取消延迟
moveEvent = $('.swiper-box').children('.swiper-alone').eq(0);
moveEvent.css('transition', 'none');
// 鼠标按住的时候停止自动播放
window.clearTimeout(clearAutoPlay);
}
function touchm(e) {
var touch = e.touches[0];
moveX = Math.floor(touch.pageX);
// 计算手指拖动的距离,并设置给第一个子元素
moveDistance = moveX - startX;
moveEvent.css('transform', 'translateX('+ moveDistance +'px)');
}
function touche(e) {
var touche = e.changedTouches[0];
endX = Math.floor(touche.pageX);
moveEvent.css('transition', 'all '+ than.autoPlaySpeed +'ms ease');
// console.log('touchend = ' + endX);
// 手指离开屏幕的时候计算当前的位置
if(moveDistance >= 120){
than.theRight();
}else if(moveDistance <= -120){
than.theLeft();
}else{
moveEvent.css('transform', 'translateX(0px)');
}
// 手指离开屏幕的时候开启自动播放
than.autoPlayFun();
}
touchMain.addEventListener('touchstart', touchs, false);
touchMain.addEventListener('touchmove', touchm, false);
touchMain.addEventListener('touchend', touche, false);
}
// 自动轮播
this.autoPlayFun = function() {
if(this.autoPlay){
let than = this;
(function swiperPlay() {
clearAutoPlay = window.setTimeout(function(){
than.theLeft();
swiperPlay();
}, than.autoPlayTime);
})();
}
}
// 运行初始函数
this.init = function(){
this.settingCss();
this.autoPlayFun();
this.manualSelect();
}
this.init();
}