HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>轮播图</title>
<link rel="stylesheet" href="css/lunbotu.css">
</head>
<body>
<div id="container">
<ul class="parent" style="left: 0;">
<li><img src="img/img1.png"></li>
<li><img src="img/img2.png"></li>
<li><img src="img/img3.png"></li>
<li><img src="img/img4.png"></li>
<li><img src="img/img5.png"></li>
<li><img src="img/img7.png"></li>
</ul>
<div class="btnLeft"><</div>
<div class="btnRight">></div>
<div class="modal">
<div class="title">
<h2>轮播图</h2>
</div>
<div class="dots">
<ul class="clearfix">
<li class="on"></li>
<li class="off"></li>
<li class="off"></li>
<li class="off"></li>
<li class="off"></li>
<li class="off"></li>
</ul>
</div>
</div>
</div>
<script src="js/lunbotu.js"></script>
</body>
</html>
JS
var imgShow = document.getElementsByClassName('parent')[0],
dotList = document.querySelectorAll('.dots >.clearfix > li');
var btnLeft = document.getElementsByClassName('btnLeft')[0],
btnRight = document.getElementsByClassName('btnRight')[0];
var dotLen = dotList.length,
index = 0;
function showRadius() {
for(var i = 0; i < dotLen; i++) {
if(dotList[i].className === "on"){
dotList[i].className = "off";
}
}
dotList[index].className = "on";
}
btnLeft.onclick = function() {
index--;
if(index < 0){
index = 5;
}
showRadius();
var left;
var imgLeft = imgShow.style.left;
if(imgLeft === "0px") {
left = -2500;
}
else{
left = parseInt(imgLeft) + 500;
}
imgShow.style.left = left + "px";
}
btnRight.onclick = function() {
index++;
if(index > 5){
index = 0;
}
showRadius();
var right;
var imgLeft = imgShow.style.left;
if(imgLeft === "-2500px") {
right = 0;
}
else{
right = parseInt(imgLeft) - 500;
}
imgShow.style.left = right + "px";
}
for(var i = 0; i < dotLen; i++) {
(function(i) {
dotList[i].onclick = function() {
var dis = index - i;
imgShow.style.left = (parseInt(imgShow.style.left) + dis * 500) + "px";
index = i;
showRadius();
}
})(i);
}
CSS
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
a {
text-decoration: none;
}
#container {
position: relative;
width: 500px;
height: 260px;
margin: 20px auto;
overflow: hidden;
}
#container .parent {
position: absolute;
width: 3000px;
height: 260px;
}
#container .parent li {
float: left;
width: 500px;
height: 100%;
}
#container .parent li img {
width: 100%;
height: 100%;
}
#container .btnLeft,
#container .btnRight {
width: 30px;
height: 30px;
background-color: #9E9E9E;
border-radius: 20%;
opacity: 80%;
position: absolute;
top: 0;
bottom: 0;
margin: auto;
font-size: 20px;
color: #f40;
text-align: center;
line-height: 30px;
}
#container .btnLeft {
left: 10px;
}
#container .btnRight {
right: 10px;
}
#container .btnLeft:hover,
#container .btnRight:hover {
opacity: 90%;
cursor: pointer;
}
#container .modal {
width: 100%;
height: 40px;
background: rgba(0, 0, 0, .3);
position: absolute;
left: 0;
bottom: 0;
line-height: 40px;
padding: 0 40px;
box-sizing: border-box;
}
#container .modal .title {
float: left;
color: #fff;
font-size: 12px;
}
#container .modal .dots {
float: right;
position: absolute;
bottom: 10px;
left: 340px;
}
#container .modal .dots li {
width: 15px;
height: 15px;
border-radius: 50%;
float: left;
margin: 0 5px;
cursor: pointer;
}
.clearfix::after {
content: "";
display: block;
clear: both;
}
.on {
background-color: red;
}
.off {
background-color: gray;
}