-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
224 lines (215 loc) · 5.29 KB
/
index.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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>原生JS实现图片轮播</title>
<style>
* {
margin: 0;
padding: 0;
text-decoration: none;
}
body {
padding: 20px;
}
#container {
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
position: relative;
margin: 0 auto;
}
#list {
width: 4200px;
height: 400px;
position: absolute;
z-index: 1;
}
#list img {
float: left;
}
#buttom {
position: absolute;
height: 10px;
width: 100px;
z-index: 2;
bottom: 20px;
left: 250px;
}
#buttom span {
cursor: pointer;
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;
background: #333;
margin-right: 5px;
}
#buttom .on {
background: orangered;
}
.arrow {
display: none;
position: absolute;
top: 180px;
cursor: pointer;
line-height: 40px;
text-align: center;
font-size: 40px;
font-weight: bold;
width: 40px;
height: 40px;
z-index: 2;
background-color: rgba(0, 0, 0, 0.3);
color: #fff;
}
.arrow:hover {
background-color: rgba(0, 0, 0, 0.7);
}
#container:hover .arrow {
display: block;
}
#prev {
left: 20px;
}
#next {
right: 20px;
}
#banner {
position: relative;
padding: 20px 0;
text-align: center;
font-family: '微软雅黑';
font-weight: bold;
font-size: 30px;
}
</style>
</head>
<body>
<p id="banner">JS原生图片轮播演示</p>
<div id="container">
<div id="buttom">
<span index="1" class='on'></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<div id="list" style='left: -600px;'>
<img src="imgs/5.jpg" alt="1" title="轮播图片">
<img src="imgs/1.jpg" alt="1" title="轮播图片">
<img src="imgs/2.jpg" alt="2" title="轮播图片">
<img src="imgs/3.jpg" alt="3" title="轮播图片">
<img src="imgs/4.jpg" alt="4" title="轮播图片">
<img src="imgs/5.jpg" alt="5" title="轮播图片">
<img src="imgs/1.jpg" alt="5" title="轮播图片">
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
<script>
window.onload = function(){ // 页面加载完后开始执行
var container = document.getElementById('container'),
list = document.getElementById('list'),
buttom = document.getElementById('buttom').getElementsByTagName('span'),
prev = document.getElementById('prev'),
next = document.getElementById('next');
index = 1,
len = 5,
animated = false,
interval = 2000;
var timer;
function animate(offset){
if (offset == 0) {
return;
}
// 加入图片动画进去效果,优化体验
animated = true;
var time = 300, //动画位移总时间
interval = 10, // 位移间隔时间
speed = offset/(time/interval), // 每次位移量
left = parseInt(list.style.left) + offset; // parseInt()解析字符串,返回整数
var go = function (){
if ((speed > 0 && parseInt(list.style.left) < left) || (speed < 0 &&parseInt(list.style.left) > left)) {
list.style.left = parseInt(list.style.left) + speed + 'px';
setTimeout(go, interval);//达到位移间隔时间后,自调go()函数,也就是运用递归不停的进行自调
} else {
list.style.left = left + 'px';
if (left > -200) {
list.style.left = -600 * len + 'px';
}
if (left < (-600 * len)) {
list.style.left = '-600px';
}
animated = false;
}
}
go();
}
function showButtom(){ //
for (var i = 0; i < buttom.length; i++) {
if (buttom[i].className == 'on') {
buttom[i].className = '';
break;
}
}
buttom[index - 1].className = 'on';
}
function play(){ // 自动播放
timer = setTimeout(function(){
next.onclick();
play();
}, interval);
}
function stop(){
clearTimeout(timer);
}
next.onclick = function(){
if (animated) {
return;
}
if (index==5) { // 归位按钮
index = 1;
} else {
index += 1;
}
animate(-600);
showButtom();
}
prev.onclick = function(){
if (animated) {
return;
}
if (index == 1) {
index = 5;
} else {
index -= 1;
}
animate(-600);
showButtom();
}
for (var i = 0; i < buttom.length; i++) {
buttom[i].onclick = function(){
if (animated) {
return;
}
if (this.className == 'on') {// 显示的当前图片和要点击的按钮对应时,不执行下列操作
return;
}
// 点击按钮,跳到对应的目标图片
var myIndex = parseInt(this.getAttribute('index'));// getAttribute() 方法返回指定属性名的属性值
var offset = -600 * (myIndex - index);
animate(offset);
index = myIndex;
showButtom();
}
}
container.onmouseover = stop; // 当鼠标移到图片上时,清除自动播放
container.onmouseout = play; // 当鼠标离开图片时,开始自动播放
play();
}
</script>
</body>
</html>