非博主原创,学习过程中,记录一下,代码依赖jquery。
HTML代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<div class="slide-container">
<div class="slide-item fade">
<img src="images/img_fjords_wide.jpg" />
<p class="slide-text">文本信息1</p>
</div>
<div class="slide-item fade">
<img src="images/img_mountains_wide.jpg" />
<p class="slide-text">文本信息2</p>
</div>
<div class="slide-item fade">
<img src="images/img_nature_wide.jpg" />
<p class="slide-text">文本信息3</p>
</div>
<a href="javascript:;" class="prev"><</a>
<a href="javascript:;" class="next">></a>
<div class="slide-num"></div>
</div>
CSS代码: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* {
margin: 0;
padding: 0
}
body {
font-family: verdana, sans-serif;
}
.slide-container {
width: 1000px;
position: relative;
margin: 50px auto;
}
.slide-item {
position: relative;
}
.slide-text {
position: absolute;
bottom: 8px;
width: 100%;
color: #fff;
font-size: 16px;
text-align: center;
}
.prev,
.next {
position: absolute;
top: 50%;
margin-top: -30px;
cursor: pointer;
color: #fff;
font-size: 30px;
font-weight: bold;
font-family: "宋体";
transition: all .6s ease;
z-index: 10;
text-decoration: none;
}
.prev {
left: 10px;
}
.next {
right: 10px;
}
.slide-num {
margin-top: 10px;
text-align: center;
}
.dot {
margin: 0 4px;
display: inline-block;
width: 14px;
height: 14px;
cursor: pointer;
background: #bbb;
border-radius: 50%;
transition: all .3s ease;
}
.dot:hover,
.dot.active {
background: #717171;
}
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
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
62var sIndex = 1; //初始化显示图片
var timer = null; //定时器
var $prev = $(".prev"),
$next = $(".next");
showSlides(sIndex);
//核心代码
function showSlides(n) {
var $slideItem = $(".slide-item");
var $slideNum = $(".slide-num");
var str = '';
//自动添加图片序号
if ($slideNum.find("span").length == 0) {
for (var i = 0; i < $(".slide-item").length; i++) {
str += '<span class="dot" onclick="currentSlide(' + (i + 1) + ')"></span>';
}
$slideNum.append(str);
}
//切换到最后一张
if (n > $slideItem.length) {
sIndex = 1;
}
//切换到第一张
if (n < 1) {
sIndex = $slideItem.length;
}
//全部隐藏
$slideItem.hide();
//只显示一张
$slideItem.eq(sIndex - 1).show();
//显示对应的图片序号
$slideNum.find("span").eq(sIndex - 1).addClass("active").siblings().removeClass("active")
}
//上一张,下一张
function plusSlides(n) {
sIndex += n;
showSlides(sIndex);
}
//序号
function currentSlide(n) {
sIndex = n;
showSlides(sIndex);
}
//上一张
$prev.click(function () {
plusSlides(-1);
})
//下一张
$next.click(function () {
plusSlides(1);
})
//自动播放
function autoShowSlide() {
sIndex++;
showSlides(sIndex);
}
timer = setInterval(autoShowSlide, 2000);
//清除定时器
$(".slide-container").hover(function () {
clearInterval(timer);
}, function () {
timer = setInterval(autoShowSlide, 2000);
})