使用js完成轮播图,自动播放,左右切换,底部圆点点击跳转,鼠标悬停
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
#box {
width: 480px;
position: relative;
margin: 0 auto;
}
img {
position: absolute;
width: 480px;
height: 270px;
color: #ffffff;
}
.text {
position: absolute;
left: calc(50% - 65px);
top: 240px;
text-align: center;
z-index: 999;
}
.monx {
float: left;
width: 20px;
height: 20px;
background-color: rgba(255, 245, 244, 0.3);
margin-left: 4px;
border: 1px solid #464646;
}
.active {
background: red;
}
#left {
background: rgb(114, 110, 110);
width: 30px;
height: 50px;
position: absolute;
left: 10px;
top: 110px;
line-height: 50px;
}
#right {
background: rgb(156, 147, 147);
width: 30px;
height: 50px;
position: absolute;
right: 10px;
top: 110px;
line-height: 50px;
text-align: center;
}
</style>
<body>
<div id="box">
<div class="img"><img src="./img/1.jpg" alt="" /></div>
<div class="img"><img src="./img/2.jpg" alt="" /></div>
<div class="img"><img src="./img/3.jpg" alt="" /></div>
<div class="img"><img src="./img/2.jpg" alt="" /></div>
<div class="img"><img src="./img/3.jpg" alt="" /></div>
<div class="text">
<span class="monx active">1</span>
<span class="monx">2</span>
<span class="monx">3</span>
<span class="monx">4</span>
<span class="monx">5</span>
</div>
<span id="left">《</span>
<span id="right">》</span>
</div>
</body>
<script>
var img = document.getElementsByClassName("img");
var box = document.getElementById("box");
var monx = document.getElementsByClassName("monx");
var left = document.getElementById("left");
var right = document.getElementById("right");
var index = 0;
var indetId;
function clean() {
for (var i = 0; i < img.length; i++) {
index = index % 5;
img[i].style.display = "none";
monx[i].className = "monx";
}
img[index].style.display = "block";
monx[index].className = "monx active";
}
goTime();
function goTime() {
indetId = setInterval(function () {
index++;
clean();
}, 1000);
}
for (var i = 0; i < monx.length; i++) {
monx[i].index = i;
monx[i].onclick = function () {
index = this.index;
clean();
};
}
left.onclick = function () {
index--;
if (index < 0) {
index = monx.length - 1;
}
clean();
};
right.onclick = function () {
index++;
if (index == monx.length) {
index = 0;
}
clean();
};
box.onmouseover = function () {
clearInterval(indetId);
};
box.onmouseout = function () {
goTime();
};
</script>
</html>
