-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathslideshow-by-own-jquery.html
294 lines (261 loc) · 8.56 KB
/
slideshow-by-own-jquery.html
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
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
<title>用自己写的类jQuery工具库实现轮播图</title>
<style type="text/css">
/* css reset*/
html,body,div,span,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,abbr,address,cite,code,del,dfn,em,img,ins,kbd,q,samp,small,strong,sub,sup,var,b,i,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,figcaption,figure,footer,header,hgroup,menu,nav,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}:focus{outline:1}article,aside,canvas,details,figcaption,figure,footer,header,hgroup,menu,nav,section,summary{display:block}nav ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:"";content:none}a{margin:0;padding:0;border:0;font-size:100%;vertical-align:baseline;background:transparent}ins{background-color:#ff9;color:#000;text-decoration:none}mark{background-color:#ff9;color:#000;font-style:italic;font-weight:700}del{text-decoration:line-through}abbr[title],dfn[title]{border-bottom:1px dotted #000;cursor:help}table{border-collapse:collapse;border-spacing:0}hr{display:block;height:1px;border:0;border-top:1px solid #ccc;margin:1em 0;padding:0}input,select{vertical-align:middle}
/* main */
html { height: 100%;}
body { background: #f0f0f0; height: 100%;}
/* 水平垂直居中-公共类 */
.c-wrap { display: table; *writing-mode: tb-rl; }
.c-inner { display: table-cell; vertical-align: middle; *width:100%; *margin:auto 0; *writing-mode:lr-tb;}
/*main*/
ul { list-style: none;}
.container { position:relative; width: 710px; height: 355px; overflow: hidden; margin:100px auto; box-shadow:0 0 8px #999;}
.slider { position:absolute; top:0; left:0;}
.slider li { float: left; width: 710px; height: 355px;}
.dots { position:absolute; bottom:20px; left:0; width: 100%; text-align: right;}
.dots li { display:inline-block; width: 13px; height: 13px; margin-right:5px; border: 2px solid #fff; background:#0AB0E8; border-radius: 50%; cursor:pointer;}
.dots li.cur { background:#F8CD47;}
.next, .prev { position:absolute; top:50%; width: 40px; height: 120px; margin-top:-60px; background:rgba(0, 0, 0, .4); color:#fff; font-size: 48px; line-height: 2.5; text-align: center; font-weight: 700; cursor:pointer;}
.next { right:0;}
.perv { left:0;}
</style>
</head>
<body>
<div class="container">
<ul class="slider">
<li><img src="images/jquery01.jpg" alt=""></li>
<li><img src="images/jquery02.jpg" alt=""></li>
<li><img src="images/jquery03.jpg" alt=""></li>
<li><img src="images/jquery04.jpg" alt=""></li>
<li><img src="images/jquery05.jpg" alt=""></li>
<li><img src="images/jquery06.jpg" alt=""></li>
</ul>
<ul class="dots dots1">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="next next1">></div>
<div class="prev prev1"><</div>
</div>
<div class="container">
<ul class="slider">
<li><img src="images/jquery01.jpg" alt=""></li>
<li><img src="images/jquery02.jpg" alt=""></li>
<li><img src="images/jquery03.jpg" alt=""></li>
<li><img src="images/jquery04.jpg" alt=""></li>
<li><img src="images/jquery05.jpg" alt=""></li>
<li><img src="images/jquery06.jpg" alt=""></li>
</ul>
<ul class="dots dots2">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="next next2">></div>
<div class="prev prev2"><</div>
</div>
<script type="text/javascript" src="jquery-own.js"></script>
<script type="text/javascript">
;(function($) {
//默认参数,放在这里成为私有对象
var defaults = {
className: 'cur',
direction: 'x',
type: 'click',
auto: true,
speed: 400,
interval: 3000
};
//私有函数
function slideshow(options) {
var self = this,
//以空对象为target,不会破坏defaults的结构
settings = $.extend({}, defaults, options),
parent = this.parent(),
children = this.children(),
size = children.length,
distance,
prop;
//根据方向进行不同设置
if (settings.direction === 'x') {
distance = parseFloat(parent.css('width'));
prop = 'margin-left';
children.css('float', 'left');
this.css('width', size * distance + 'px')
} else {
distance = parseFloat(parent.css('height'));
prop = 'margin-top';
this.css('height,', size * distance + 'px');
}
var nodeList = [];
//将原始顺序记录下来
children.each(function() {
nodeList.push($(this));
});
var switchs, switchsList;
if (typeof settings.switchs === 'string') {
switchs = document.querySelector(settings.switchs);
switchsList = $(switchs).children();
switchsList.eq(0).addClass('cur');
if (switchs) {
var nodeName = switchsList[0].nodeName;
//切换选项区采用事件代理
switchs.addEventListener(settings.type, function(e) {
if (!animated) return;
var target = e.target,
index;
if (target.nodeName === nodeName) {
target = $(target);
index = target.index();
method.change(index).choice(index);
}
}, false);
}
}
var animated = true;
var count = 0;
var _count = 0;
var method = {
//上一帧
prev: function() {
--count;
count = count < 0 ? count + size : count;
if (_count) {
return method.choice(count);
} else {
method.change(count);
}
animated = false;
var last = self.last();
var propObj = {};
propObj[prop] = 0;
self.prepend(last.css(prop, -distance + 'px')[0]);
last.animate(propObj, settings.speed, function() {
animated = true;
});
},
//下一帧
next: function() {
count = (count + 1) % size;
if (_count) {
return method.choice(count);
} else {
method.change(count);
}
animated = false;
var first = self.first();
var propObj = {};
propObj[prop] = -distance;
first.animate(propObj, settings.speed, function() {
self.append(first.css(prop, '0px')[0]);
animated = true;
});
},
//用户自主选择某一帧
choice: function(index) {
method.change(count = index);
var target = nodeList[index];
_count = index = target.index();
var propObj = {};
propObj[prop] = -distance * index;
animated = false;
self.animate(propObj, settings.speed, function() {
animated = true;
});
},
//切换项更新
change: function(index) {
switchsList.eq(index).addClass('cur').siblings().removeClass('cur');
return this;
}
};
//是否自动播放
if (settings.auto) {
var timer;
function loop() {
timer = setInterval(method.next, settings.interval);
}
parent = parent[0];
//进入其父元素时暂停播放
parent.addEventListener('mouseover', function(e) {
e.preventDefault();
clearInterval(timer);
}, false);
//离开其父元素时重新播放
parent.addEventListener('mouseout', function(e) {
e.preventDefault();
loop();
}, false);
loop()
}
//下一帧箭头点击事件
if (typeof settings.next === 'string') {
settings.next = [].slice.call(document.querySelectorAll(settings.next));
$.each(settings.next, function() {
this.addEventListener('click', function(e) {
e.preventDefault();
if (!animated) return;
method.next();
}, false);
});
}
//上一帧箭头点击事件
if (typeof settings.prev === 'string') {
settings.prev = [].slice.call(document.querySelectorAll(settings.prev));
$.each(settings.prev, function() {
this.addEventListener('click', function(e) {
e.preventDefault();
if (!animated) return;
method.prev();
}, false);
});
}
}
//以插件形式使用
$.fn.slideshow = function() {
var args = [].slice.call(arguments);
return this.each(function(i) {
slideshow.call($(this), args[i]);
});
};
}(jQuery));
$(function() {
$('.slider').slideshow({
switchs: '.dots1',
prev: '.prev1',
next: '.next1'
}, {
//选项卡
switchs: '.dots2',
//选项卡事件
type: 'mouseover',
//上一帧
prev: '.prev2',
//下一帧
next: '.next2',
//默认也是自动播放
auto: true,
//滑动方向
direction: 'y',
//播放间隔
interval: 1000,
//滑动速率
speed: 200
});
});
</script>
</body>
</html>