-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
267 lines (229 loc) · 6.38 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
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
;(function($) {
var Carousel = function(poster) {
//console.log(poster.attr("data-setting"));
var self = this;
//保存单个旋转木马对象
this.poster = poster;
this.posterItemMain = poster.find("ul.poster-list");
this.nextBtn = poster.find("div.poster-next-btn");
this.prevBtn = poster.find("div.poster-prev-btn");
this.posterItems = poster.find('li.poster-item');
if(this.posterItems.size()/2==0) {
this.posterItemMain.append(this.posterItems.eq(0).clone());
this.posterItems = this.posterItemMain.children();
}
this.posterFirstItem = this.posterItems.eq(0);
this.posterLastItem = this.posterItems.last();
this.rotateFlag = true;
//配置默认参数
this.setting = {
"width":1000,//幻灯片宽度
"height":351,//幻灯片高度
"posterWidth":800,//幻灯片第一张宽度
"posterHeight":351,//幻灯片第一张高度
"scale":0.9,
"speed":500,
"autoplay":false,
"delay":2000,
"verticalAlign":"middle"
};
$.extend(this.setting,this.getSetting());
this.setSettingValue();
this.setPosterPos();
this.nextBtn.click(function() {
if(self.rotateFlag) {
self.rotateFlag = false;
self.carouseRotate("left");
}
});
this.prevBtn.click(function() {
if(self.rotateFlag) {
self.rotateFlag = false;
self.carouseRotate("right");
}
});
//是否播放自动播放
if(this.setting.autoplay) {
this.autoPlay();
this.poster.hover(function() {
window.clearInterval(self.timer);
}, function() {
self.autoPlay();
});
}
//console.log(this.setting);
}
Carousel.prototype = {
autoPlay:function() {
var self = this;
this.timer = window.setInterval(function() {
self.nextBtn.click();
},this.setting.delay);
},
//设置剩余广告的位置关系
setPosterPos:function() {
var self = this;
//获取到除第一张外的其它li
var sliceItems = this.posterItems.slice(1);
var sliceSize = sliceItems.size()/2;
//右边的个数
var rightSlice = sliceItems.slice(0,sliceSize);
var level = Math.floor(this.posterItems.size()/2);
//左边
var leftSlice = sliceItems.slice(sliceSize);
var rw = this.setting.posterWidth;
var rh = this.setting.posterHeight;
var gap = ((this.setting.width-this.setting.posterWidth)/2)/level;
var firstLeft = (this.setting.width-this.setting.posterWidth)/2;
var fixOffsetLeft = firstLeft+rw;
//设置右边帧的位置和宽度高度
rightSlice.each(function(i) {
level--;
rw = rw*self.setting.scale;
rh = rh*self.setting.scale;
var j = i;
$(this).css({
zIndex:level,
width:rw,
height:rh,
opacity:1/(++i),
left:fixOffsetLeft+(++j)*gap-rw,
top:self.setVerticalAlign(rh)
});
});
//设置左边的
var lw = rightSlice.last().width();
var lh = rightSlice.last().height();
var oloop = Math.floor(this.posterItems.size()/2);
leftSlice.each(function(i) {
$(this).css({
zIndex:i,
width:lw,
height:lh,
opacity:1/oloop,
left:i*gap,
top:self.setVerticalAlign(lh)
});
lw = lw/self.setting.scale;
lh = lh/self.setting.scale;
oloop--;
})
},
//设置垂直排列方式
setVerticalAlign:function(height) {
var verticalType = this.setting.verticalAlign;
var top = 0;
if(verticalType==="middle"){
top = (this.setting.height-height)/2;
} else if(verticalType === "top") {
top = 0;
} else if(verticalType === "bottom") {
top = this.setting.height-height;
} else {
top = (this.setting.height-height)/2;
}
return top;
},
//设置配置参数数值去控制基本的宽度高度
setSettingValue:function() {
this.poster.css({
width:this.setting.width,
height:this.setting.height
});
this.posterItemMain.css({
width:this.setting.width,
height:this.setting.height
});
//计算上下切换按钮的宽度
var w = (this.setting.width-this.setting.posterWidth)/2;
this.nextBtn.css({
width:w,
height:this.setting.height,
zIndex:Math.ceil(this.posterItems.size()/2)
});
this.prevBtn.css({
width:w,
height:this.setting.height,
zIndex:Math.ceil(this.posterItems.size()/2)
});
this.posterFirstItem.css({
width:this.setting.posterWidth,
height:this.setting.posterHeight,
left:w,
zIndex:Math.floor(this.posterItems.size()/2)
})
},
//获取人工配置的参数
getSetting:function() {
var setting = this.poster.attr("data-setting");
//转换成json对象
if(setting&&setting!="") {
return $.parseJSON(setting);
} else {
return {};
}
},
carouseRotate:function(dir) {
var _this_ = this;
var zIndexArr = [];
if(dir === "left") {
this.posterItems.each(function() {
var self = $(this);
var prev = self.prev().get(0)?self.prev():_this_.posterLastItem;
var width = prev.width();
var height = prev.height();
var zIndex = prev.css("zIndex");
var opacity = prev.css("opacity");
var left = prev.css("left");
var top = prev.css("top");
zIndexArr.push(zIndex);
self.animate({
width:width,
height:height,
opacity:opacity,
left:left,
top:top
},_this_.setting.speed,function() {
_this_.rotateFlag = true;
});
});
this.posterItems.each(function(i) {
$(this).css("zIndex",zIndexArr[i]);
});
} else if (dir === "right") {
this.posterItems.each(function() {
var self = $(this);
var next = self.next().get(0)?self.next():_this_.posterFirstItem;
var width = next.width();
var height = next.height();
var zIndex = next.css("zIndex");
var opacity = next.css("opacity");
var left = next.css("left");
var top = next.css("top");
zIndexArr.push(zIndex);
self.animate({
width:width,
height:height,
//zIndex:zIndex,
opacity:opacity,
left:left,
top:top
},_this_.setting.speed,function() {
_this_.rotateFlag = true;
});
});
this.posterItems.each(function(i) {
$(this).css("zIndex",zIndexArr[i]);
});
}
}
};
//init用来初始化所有传进来的集合的
Carousel.init = function(posters) {
var _this = this;//用_this来保存Carousel
posters.each(function() {
new _this($(this));//new的是Carousel,这个里面的this就是posters里的每一个,并包装成jquery对象
})
}
window["Carousel"] = Carousel;//全局注册,否则外面无法访问到
})(jQuery);