<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>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
#content {
width: 750px;
height: 300px;
margin: 30px;
border: 1px solid #000;
position: relative;
}
.left,
.right {
position: absolute;
top: 50%;
transform: translateY(-50%);
padding: 5px 6px;
font-size: 36px;
font-weight: bold;
background-color: black;
opacity: 0.4;
color: white;
cursor: pointer;
}
.right {
right: 0;
}
.slide-nav {
display: flex;
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
li {
border-radius: 50%;
margin-right: 10px;
cursor: pointer;
border: solid 5px white;
}
.on {
border-color: red;
}
</style>
</head>
<body>
<div id="content">
<img src="./images/1.png" alt="" id="img">
<div class="left"><</div>
<div class="right">></div>
<ul class="slide-nav">
<li class="on"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<script>
var content = document.getElementById("content");
var img = document.getElementById("img");
var left = document.querySelector(".left");
var right = document.querySelector(".right");
var lis = document.getElementsByTagName("li");
console.log(left, right);
var arr = ["./images/1.png", "./images/2.png", "./images/3.png", "./images/4.png", "./images/5.png", "./images/6.png"];
var num = 0;
function change(){
img.src = arr[num];
for(var i = 0;i<lis.length;i++){
lis[i].classList.remove("on")
}
lis[num].classList.add("on")
}
var timer = setInterval(slide, 1000);
function slide() {
num++;
if (num === arr.length) {
num = 0;
}
change()
}
content.onmouseover = function () {
clearInterval(timer);
left.style.opacity = "0.8"
right.style.opacity = "0.8"
}
content.onmouseout = function () {
timer = setInterval(slide, 1000)
left.style.opacity = "0.4"
right.style.opacity = "0.4"
}
left.onclick = function () {
num--;
if (num < 0) {
num = arr.length - 1;
}
change()
}
right.onclick = function () {
slide()
}
for (var i = 0; i < lis.length; i++) {
lis[i].index = i;
lis[i].onclick = function () {
num = this.index;
change()
}
}
</script>
</body>
效果图