-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSliderAuto.js
More file actions
286 lines (240 loc) · 7.39 KB
/
Copy pathSliderAuto.js
File metadata and controls
286 lines (240 loc) · 7.39 KB
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
;(function(){
function extend(defaultSetting,userSetting){
//创建一个空对象
var obj={};
for(var p in defaultSetting){
if(userSetting.hasOwnProperty(p) && userSetting[p] != null){ //(1)
obj[p] = userSetting[p];
}
else{ //(2)
obj[p] = defaultSetting[p];
}
}
return obj;
}
function Slider(contentId,userSetting={}) {
var that = this;
var defaultSetting = {
speed:2000,
currentIndex:0,
isAuto:true,
isTxt:true,
directorPos:"center"
}
this.setting = extend(defaultSetting,userSetting);//综合考虑用户的设置和默认值后得到的。
console.info(this.setting)
this.timer = null; //定时器
// 获取整体轮播图的容器
// if(contentId是字符串)
// 通过id去搜索
// if(本身就是dom元素)
// 则直接使用
if(typeof contentId == "string"){
this.container = document.getElementById(contentId);
if(this.container == null){
console.info("没有这个id,你看清楚再来");
return {};
}
}
else if(typeof contentId == "object")
{
this.container = contentId;
}
// 获取图片集合
this.content = this.container.querySelector(".slider-content");
this.content.addEventListener("transitionend",function(){
console.info(that.isback)
if(that.isback){
that.content.style.transition="";
that.content.style.transform = "translateX(-800px)";
that.isback = false;
}
else if(that.isfront){
that.content.style.transition="";
that.content.style.transform = "translateX(-"+800*(that.len)+"px)";
that.isfront = false;
}
})
this.lis = this.container.querySelector(".slider-content").querySelectorAll("li");
this.content.style.width = this.lis.length * this.lis[0].offsetWidth +"px";
this.len = this.lis.length - 2;//图片的张数
this.directors = []; //角标;
this.currentIndex = this.setting.currentIndex; //当前显示的那张图的编号
this.speed = this.setting.speed; // 每隔2s,切换一张图 2000ms 单位是ms
//this.dom();
dom.call(this); //动态创建dom元素
//this.init();
init.call(this); //this.初始化,绑定一些事件
this.goto(this.currentIndex); //显示当前图片
// if(this.setting.isAuto)
// {
// this.auto();
// }
this.setting.isAuto && this.auto(); //开始自动播放
}
function dom(){
var that = this;
//创建按钮,设置属性,添加事件响应,添加到外部的容器中
this.btnRight = document.createElement("span");
this.btnRight.className = "btn btn-right";
this.btnRight.innerHTML = "";
this.btnRight.onclick =function(){
that.next();
}
this.btnLeft = document.createElement("span");
this.btnLeft.className = "btn btn-left";
this.btnLeft.innerHTML = "";
this.btnLeft.onclick =function(){
that.prev();
}
this.container.appendChild(this.btnRight);
this.container.appendChild(this.btnLeft);
//创建文字区域,设置属性,添加到外部的容器中
if( this.setting.isTxt){
this.txtArea = document.createElement("p");
this.txtArea.className = "txt";
this.txtArea.innerHTML = "";
this.container.appendChild(this.txtArea);
}
//先创建角标的容器 ol
var ol = document.createElement("ol");
ol.className = "slider-director";
if(this.setting.directorPos == "center"){
ol.style.left = "50%";
ol.style.right="auto";
ol.style.transform = "translateX(-50%) ";
}
else if(this.setting.directorPos == "right"){
ol.style.left = "auto";
ol.style.right="20px";
}
else{
ol.style.left = "50%";
ol.style.right="auto";
ol.style.transform = "translateX(-50%) ";
}
for(var i=0;i<this.len;i++){
//用循环去创建多个li
var li = document.createElement("li");
li.innerHTML = i+1;
this.directors.push(li);
//添加到ol中
ol.appendChild(li);
}
//ol添加到轮播图的容器中
this.container.appendChild(ol);
}
function init(){
// console.info("init",this)
var that = this;
for (var i = 0; i < this.directors.length; i++) {
this.directors[i].onmouseover = function(abc){
return function(){
that.goto(abc);
}
}(i)
// this.directors[i].abc = i;
// this.directors[i].onmouseover = function(){
// that.goto(this.abc);
// }
}
this.container.onmouseenter = function(){
that.pause();
}
this.container.onmouseleave = function(){
that.continue();
}
}
// 上一张()
Slider.prototype.prev = function(){
//更新currentIndex
this.currentIndex -= 1;
if(this.currentIndex < 0){
this.goto(this.currentIndex);
this.isfront = true;
this.currentIndex = this.len - 1;
}
else{
this.goto(this.currentIndex);
}
}
//下一张()
Slider.prototype.next = function(){
//更新currentIndex
this.currentIndex += 1;
if(this.currentIndex >= this.len){
this.goto(this.currentIndex);
this.isback = true;
this.currentIndex = 0;
}
else{
this.goto(this.currentIndex);
this.isback = false;
}
}
//跳到指定张 n:[0,this.len-1]
Slider.prototype.goto = function (n) {
this.currentIndex = n ; //更新currentIndex
//console.info(this.currentIndex);
this.content.style.transition = "transform .5s";
this.content.style.transform = "translateX(-"+ (n+1)*800 +"px)";
for(var i =0; i<this.len;i++){
this.directors[i].className = ""; //清除所有的className
}
this.directors[(n+this.len)%this.len].className="current";
//更新p标签中的内容
this.txtArea && ( this.txtArea.innerHTML = this.lis[(n+1)%this.len].querySelector("img").getAttribute("alt") );//当前图片中的alt属性
}
// Slider.prototype.goto = function (n) {
// this.currentIndex = n ; //更新currentIndex
// //console.info(this.currentIndex);
// // 把所有的图片改成display:none
// for(var i = 0;i<this.len;i++){
// this.lis[i].style.opacity = 0;
// //this.lis[i].style.display="none";
// }
// // 把当前currentIndex改成display:block;
// // this.lis[n].style.display = "block";
// this.lis[n].style.opacity = 1;
// for(var i =0; i<this.len;i++){
// this.directors[i].className = ""; //清除所有的className
// }
// this.directors[n].className="current";
// //更新p标签中的内容
// this.txtArea && ( this.txtArea.innerHTML = this.lis[n].querySelector("img").getAttribute("alt") );//当前图片中的alt属性
// }
//自动播放()
Slider.prototype.auto = function () {
var that = this;
clearInterval(this.timer);
this.timer = setInterval(function(){
that.next();
},this.speed);
}
//暂停播放()
Slider.prototype.pause = function(){
clearInterval(this.timer)
}
//继续方法()
Slider.prototype.continue = function(){
this.auto();
}
window.Slider = Slider;
})();
window.onload = function(){
//找到全部的具有slider类的容器
var sliders = document.querySelectorAll(".slider");
//console.info(sliders);
for(var i=0;i<sliders.length;i++){
// console.info(sliders[i]);
var userSetting = {};
userSetting.speed = sliders[i].getAttribute("data-speed");
userSetting.currentIndex = sliders[i].getAttribute("data-currentIndex");
userSetting.isAuto = sliders[i].getAttribute("data-isAuto") == "true";
userSetting.isTxt = sliders[i].getAttribute("data-isTxt") == "true";
userSetting.directorPos = sliders[i].getAttribute("data-directorPos");
console.info(userSetting);
//new Slider(sliders[i],userSetting);
new Slider(sliders[i],sliders[i].dataset);
}
}