-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path轮播图.html
199 lines (198 loc) · 6.19 KB
/
轮播图.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>焦点轮播图</title>
<script src="../tools.js"></script>
<style>
* {
margin: 0px;
padding: 0px;
text-decoration: none;
}
.container {
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
position: relative;
margin: 50px auto;
}
.container .list {
width: 4200px;
height: 400px;
position: absolute;
/* left: -600px; */
z-index: 1;
}
.container .list img {
float: left;
width: 600px;
height: 400px;
}
.container .buttons {
position: absolute;
height: 10px;
width: 100px;
z-index: 2;
bottom: 20px;
left: 250px;
}
.container .buttons span {
cursor: pointer;
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;
background: #333;
margin-right: 5px;
}
.container .buttons .on {
background-color: orangered;
}
.container a {
cursor: pointer;
display: none;
line-height: 39px;
text-align: center;
font-size: 36px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
top: 180px;
z-index: 2;
background-color: rgba(0, 0, 0, 0.3);
color: #fff;
}
.container a:hover {
background-color: rgba(0, 0, 0, 0.7);
}
.container:hover a {
display: block;
}
.container .prev{
left: 20px;
}
.container .next {
right: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="list" style="left: -600px">
<img src="dog5.jpeg" alt="">
<img src="dog1.jpeg" alt="">
<img src="dog2.jpeg" alt="">
<img src="dog3.jpeg" alt="">
<img src="dog4.jpeg" alt="">
<img src="dog5.jpeg" alt="">
<img src="dog1.jpeg" alt="">
</div>
<div class="buttons">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a href="#" class="prev"><</a>
<a href="#" class="next">></a>
</div>
<script>
var container = document.getElementsByClassName('container')[0];
var list = document.getElementsByClassName('list')[0];
var buttons = document.getElementsByTagName('span');
var prev = document.getElementsByClassName('prev')[0];
var next = document.getElementsByClassName('next')[0];
var index = 1; // 用于显示当前第几张图片
// var len = 5;
var animated = false;//判断图片是否正在切换中,图片没切换才让图片切换,在切换过程中等待其切换完毕再切换
// var interval = 3000;
var timer;
//用于亮起小圆点功能
function showButton() {
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className == 'on') {
buttons[i].className = '';
break;
}
}
buttons[index - 1].className = 'on';
}
//图片切换功能
function animate(offset) {
animated = true; //图片正在切换
var newLeft = parseInt(getStyle(list, 'left')) + offset;
var time = 300;//总位移时间
var interval = 15;//位移间隔时间
var speed = offset / (time / interval);//每次位移量
function go() {
if ((speed < 0 && parseInt(list.style.left) > newLeft)|| (speed > 0 && parseInt(list.style.left) < newLeft)) {
list.style.left = parseInt(list.style.left) + speed + 'px';
setTimeout(go, interval)
} else {
animated = false;
list.style.left = newLeft + 'px';
if (newLeft < -3000) {
list.style.left = '-600px';
}
if (newLeft > -600) {
list.style.left = '-3000px';
}
}
}
go();
}
//自动播放
function play() {
timer = setInterval(function() {
next.click();
}, 3000)
}
//鼠标移动到录播图上停止自动播放
function stop() {
clearInterval(timer);
}
addEvent(next, 'click', function() {
if (!animated) {
if (index == 5) {
index = 1;
} else {
index ++;
}
showButton();
animate(-600);
}
})
addEvent(prev, 'click', function() {
if (!animated) {
if (index == 1) {
index = 5;
} else {
index --;
}
showButton()
animate(600);
}
})
for (var i = 0; i < buttons.length; i++) {
addEvent(buttons[i], 'click', function() {
if (this.className == 'on') {
return;
}
var newIndex = parseInt(this.getAttribute('index'));
offset = -600 * (newIndex - index);
animate(offset);
index = newIndex;
showButton();
})
}
play();//一开始自动播放
addEvent(container, 'mouseover', stop);//鼠标移到轮播图上就停止自动播放
addEvent(container, 'mouseout', play);//鼠标移出去就自动播放
</script>
</body>
</html>