本文实例为大家分享了jquery插件实现轮播图的具体代码,供大家参考,具体内容如下
轮播图的实现(jquery插件)
需要引入jquery插件,去jquery官网搜索
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
|
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title >Document</ title >
< style >
* {
margin: 0;
padding: 0;
list-style: none;
}
img {
display: block;
}
.slider {
height: 340px;
width: 790px;
margin: 100px auto;
position: relative;
overflow: hidden;
}
.slider li {
position: absolute;
display: none;
}
.slider li:first-child {
display: block;
}
/*文字部分的样式*/
.slider li p {
position: absolute;
width: 100%;
padding: 10px 0;
text-indent: 2em;
background-color: rgba(0, 0, 0, .6);
bottom: 0;
left: 0;
font-size: 18px;
/*给文字设置位移到底部去*/
transform: translate3d(0, 100%, 0);
/*添加过渡*/
transition: all .8s;
}
/*当前展示li 下的文字样式*/
.slider li.current p {
/*文字位移到0的位置,正常显示*/
transform: translate3d(0, 0, 0);
}
.slider li a {
color: #fff;
}
.arrow {
display: none;
}
.slider:hover .arrow {
display: block;
}
.arrow-left,
.arrow-right {
font-family: "SimSun", "宋体";
width: 30px;
height: 60px;
background-color: rgba(0, 0, 0, 0.1);
position: absolute;
top: 50%;
margin-top: -30px;
cursor: pointer;
text-align: center;
line-height: 60px;
color: #fff;
font-weight: 700;
font-size: 30px;
}
.arrow-left:hover,
.arrow-right:hover {
background-color: rgba(0, 0, 0, .5);
}
.arrow-left {
left: 0;
}
.arrow-right {
right: 0;
}
</ style >
</ head >
< body >
< div class = "slider" >
< ul >
< li >
< a href = "#" >
< img src = "image/1.jpg" alt = "" >
< p >这是第1张图片的文字说明</ p >
</ a >
</ li >
< li >
< a href = "#" >
< img src = "image/2.jpg" alt = "" >
< p >这是第2张图片的文字说明</ p >
</ a >
</ li >
< li >
< a href = "#" >
< img src = "image/3.jpg" alt = "" >
< p >这是第3张图片的文字说明</ p >
</ a >
</ li >
< li >
< a href = "#" >
< img src = "image/4.jpg" alt = "" >
< p >这是第4张图片的文字说明</ p >
</ a >
</ li >
< li >
< a href = "#" >
< img src = "image/5.jpg" alt = "" >
< p >这是第5张图片的文字说明</ p >
</ a >
</ li >
< li >
< a href = "#" >
< img src = "image/6.jpg" alt = "" >
< p >这是第6张图片的文字说明</ p >
</ a >
</ li >
< li >
< a href = "#" >
< img src = "image/7.jpg" alt = "" >
< p >这是第7张图片的文字说明</ p >
</ a >
</ li >
< li >
< a href = "#" >
< img src = "image/8.jpg" alt = "" >
< p >这是第8张图片的文字说明</ p >
</ a >
</ li >
</ ul >
<!--箭头-->
< div class = "arrow" >
< span class = "arrow-left" ><</ span >
< span class = "arrow-right" >></ span >
</ div >
</ div >
< script src = "../jquery-2.2.4.js" ></ script > //jquery插件
< script >
//1、定义一个索引,且设置第一张图片为默认
var index = 0;
$('ul>li').eq(index).addClass('current').siblings().removeClass('current');
//2、右边按钮的点击
$('.arrow-right').click(function(){
index++;
//判断图片是否超过最后一张,则重新赋值
if(index == $('ul>li').length){
index = 0;
}
showImage();
})
左边按钮的实现
$('.arrow-left').click(function(){
index--;
if(index < 0 ) index = $('ul>li').length - 1;
showImage()
});
function showImage(){
$('ul>li').eq(index).addClass('current').fadeIn().siblings().removeClass('current').fadeOut();
}
//实现自动轮播
var times = '';
//鼠标移入停止播放
$('.slider').mouseover(function(){
clearInterval(times)
})
//鼠标移出,停止
$('.slider').mouseout(function(){
times = setInterval(function(){
$('.arrow-right').click();
},3000)
});
$('.slider').mouseout();
</ script >
</ body >
</ html >
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/gyy520gyy/article/details/109145015