超级简单的轮播图插件
心血来潮,写了一个简单的轮播图插件,带有左右切换功能、底部导航功能、自动循环功能。代码如下:
1 |
|
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
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
}
ul {
padding: 0;
margin: 0;
}
ul>li {
list-style: none;
}
.carousel-container {
position: relative;
overflow: hidden;
width: 1200px;
margin: auto;
height: 600px;
}
.carousel-list {
clear: both;
position: relative;
width: 100%;
height: 100%;
}
.carousel-container:hover .carousel-arrow {
visibility: visible;
opacity: 1;
}
.carousel-list>li {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 1;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
transition: left .3s ease-in-out, opacity .3s ease-in-out;
font-size: 72px;
color: #fff;
text-align: center;
line-height: 600px;
}
.carousel-arrow {
position: absolute;
top: 50%;
cursor: pointer;
transform: translateY(-50%);
transition: background-color .3s ease-in-out, opacity .3s ease-in-out;
z-index: 999;
padding: 5px;
visibility: hidden;
opacity: 0;
}
.carousel-arrow:hover {
background-color: rgba(0, 0, 0, 0.5);
}
.carousel-arrow.left {
left: 20px;
}
.carousel-arrow.right {
right: 20px;
}
.carousel-nav {
clear: both;
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
z-index: 999;
}
.carousel-nav>li {
float: left;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #fff;
transition: background-color .3s ease-in-out;
cursor: pointer;
}
.carousel-nav>li:hover {
background-color: #72c5ea;
}
.carousel-nav>li.active {
background-color: #49b8ea;
}
.carousel-nav>li+li {
margin-left: 10px;
}
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./css/carousel.css">
<title>轮播图</title>
</head>
<body>
<div class="carousel-container mCarousel">
<ul class="carousel-list mCarouselContent"></ul>
<div class="carousel-arrow left mPrevBtn">
<img src="./images/left.png" alt="">
</div>
<div class="carousel-arrow right mNextBtn">
<img src="./images/right.png" alt="">
</div>
<ul class="carousel-nav mCarouselNav"></ul>
</div>
<script src="./js/jquery-3.3.1.js"></script>
<script src="./js/carousel.js"></script>
<script>
$(".mCarousel").Carousel({
contanier: ".mCarouselContent",
nav: ".mCarouselNav",
prev: ".mPrevBtn",
next: ".mNextBtn",
navActiveClass: "active",
images: ["./images/img1.jpg", "./images/img2.jpg", "./images/img3.jpg"],
intervals: 3000,
auto: true
});
</script>
</body>
</html>