-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
235 lines (208 loc) · 9.4 KB
/
carousel.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
window.Carousel = (function() {
// 定义Carousel对象
var Carousel = {},
carousel = null,
left = null,
right = null,
item = null,
li_list = null,
item_active_index = -1,
item_next_index = -1,
item_prev_index = -1,
li_active = null,
li_next = null,
li_prev = null;
// I.定义一个TimerManager类
// 1)构造函数
function TimerManager() {
this.timers = []; // 保存定时器
this.args = []; // 保存定时器的参数
this.isFiring = false;
}
// 2)静态方法:为element添加一个TimerManager实例
TimerManager.makeInstance = function(element) {
// 如果element.__TimerManager__上没有TimerManager,就为其添加一个
if (!element.__TimerManager__ || element.__TimerManager__.constructor != TimerManager) {
element.__TimerManager__ = new TimerManager();
}
};
// 3)扩展TimerManager原型,分别实现add,fire,next方法
TimerManager.prototype.add = function(timer, args) {
this.timers.push(timer);
this.args.push(args);
this.fire();
};
TimerManager.prototype.fire = function() {
if ( !this.isFiring ) {
var timer = this.timers.shift(), // 取出定时器
args = this.args.shift(); // 取出定时器参数
if (timer && args) {
this.isFiring = true;
// 传入参数,执行定时器函数
timer(args[0], args[1],args[2]);
}
}
};
TimerManager.prototype.next = function() {
this.isFiring = false;
this.fire();
};
// 定义左右移动函数
function slideLeft (ele,left,callback){
var current_left = ele.offsetLeft,
width = ele.offsetWidth,
incream = width/(500/10);
setLeft(current_left);
function setLeft(current_left){
var timer = setTimeout(function(){
current_left -=incream;
ele.style.left = current_left+'px';
clearInterval(timer);
if(-current_left>=-left){
callback();
if (ele.__TimerManager__ && ele.__TimerManager__.constructor == TimerManager) {
ele.__TimerManager__.next();
}
} else {
setLeft(current_left);
}
},10);
}
}
function slideRight (ele,left,callback){
var current_left = ele.offsetLeft,
width = ele.offsetWidth,
incream = width/(500/10);
setLeft(current_left);
function setLeft(current_left){
var timer = setTimeout(function(){
current_left +=incream;
ele.style.left = current_left+'px';
clearInterval(timer);
if(current_left>=left){
callback();
if (ele.__TimerManager__ && ele.__TimerManager__.constructor == TimerManager) {
ele.__TimerManager__.next();
}
} else {
setLeft(current_left);
}
},10);
}
}
Carousel.prev = function(type,prev){
if(type=='direction'){
item_prev_index = item_active_index>0?item_active_index-1:0;
} else if(type=='mouseover'){
item_prev_index = prev;
}
addClass(item[item_prev_index],'prev');
li_active = li_list[item_active_index];
li_prev = li_list[item_prev_index];
item[item_active_index].offsetWidth;
}
Carousel.next = function(type,next){
if(type=='direction'){
item_next_index = item_active_index<item.length?item_active_index+1:item_active_index;
} else if(type=='mouseover'){
item_next_index = next;
}
addClass(item[item_next_index],'next');
li_active = li_list[item_active_index];
li_next = li_list[item_next_index];
item[item_active_index].offsetWidth;
}
// III.定义外部访问接口
Carousel.swiper = function(id) {
carousel = document.getElementById(id);
left = carousel.querySelectorAll('.left')[0];
right = carousel.querySelectorAll('.right')[0];
item = carousel.querySelectorAll('.item');
li_list = document.querySelectorAll('.btn-dot .li');
for(var i=0;i<item.length;i++){
var ele = item[i];
if(hasClass(ele,'active')){
item_active_index = i;
}
}
var that = this;
addEvent(left,'click',function(e){
that.prev('direction');
var active = item[item_active_index],
prev = item[item_prev_index];
TimerManager.makeInstance(active);
active.__TimerManager__.add(slideRight, [active,active.offsetWidth,function(){
}]);
TimerManager.makeInstance(prev);
prev.__TimerManager__.add(slideRight, [prev,0,function(){
removeClass(active,'active');
removeClass(li_active,'active');
item_active_index = item_prev_index;
addClass(prev,'active');
addClass(li_prev,'active');
removeClass(prev,'prev');
}]);
})
addEvent(right,'click',function(e){
that.next('direction');
var active = item[item_active_index],
next = item[item_next_index];
TimerManager.makeInstance(active);
active.__TimerManager__.add(slideLeft, [active,-active.offsetWidth,function(){
}]);
TimerManager.makeInstance(next);
next.__TimerManager__.add(slideLeft, [next,0,function(){
removeClass(active,'active');
removeClass(li_active,'active');
item_active_index = item_next_index;
addClass(next,'active');
addClass(li_next,'active');
removeClass(next,'next');
}]);
})
for(var i=0;i<li_list.length;i++){
var li = li_list[i];
test(i);
function test(i){
addEvent(li,'click',function(){
if(i>item_active_index&&i<item.length){
that.next('mouseover',i);
var active = item[item_active_index],
next = item[item_next_index];
TimerManager.makeInstance(active);
active.__TimerManager__.add(slideLeft, [active,-active.offsetWidth,function(){
}]);
TimerManager.makeInstance(next);
next.__TimerManager__.add(slideLeft, [next,0,function(){
removeClass(active,'active');
removeClass(li_active,'active');
item_active_index = item_next_index;
addClass(next,'active');
addClass(li_next,'active');
removeClass(next,'next');
}]);
} else if(i>=0&&i<item_active_index){
that.prev('mouseover',i);
var active = item[item_active_index],
prev = item[item_prev_index];
TimerManager.makeInstance(active);
active.__TimerManager__.add(slideRight, [active,active.offsetWidth,function(){
}]);
TimerManager.makeInstance(prev);
prev.__TimerManager__.add(slideRight, [prev,0,function(){
removeClass(active,'active');
removeClass(li_active,'active');
item_active_index = item_prev_index;
addClass(prev,'active');
addClass(li_prev,'active');
removeClass(prev,'prev');
}]);
}
})
}
}
return this;
};
// 返回Carousel对象
return Carousel;
})();