计时器停止逻辑需要优化!
<!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>
<style>
* {
padding: 0;
margin: 0;
}
.trottingHorseLamp {
width: 800px;
height: 400px;
position: relative;
}
.list {
width: 800px;
height: 400px;
list-style: none;
position: relative;
}
.item {
width: 100%;
height: 100%;
position: absolute;
opacity: 0;
-webkit-transition: all 0.5s;
transition: all 0.5s;
}
img {
width: 100%;
height: 100%;
}
.btn {
width: 50px;
height: 100px;
position: absolute;
top: 150px;
z-index: 100;
}
#shang {
left: 0px;
}
#xia {
right: 0px;
}
.item.active {
z-index: 10;
opacity: 1;
}
.pointList {
list-style: none;
position: absolute;
right: 20px;
bottom: 20px;
z-index: 1000;
}
.point {
width: 10px;
height: 10px;
background-color: rgba(0, 0, 0, 0.4);
border-radius: 100%;
float: left;
margin-right: 10px;
border: 2px solid rgba(255, 255, 255, 0.6);
cursor: pointer;
}
.point.active {
background-color: rgba(255, 255, 255, 0.4);
}
</style>
</head>
<body>
<div class="trottingHorseLamp">
<ul class="list">
<li class="item active"><img src="./1minx4.jpg" alt="" /></li>
<li class="item"><img src="./xiaomi.webp" alt="" /></li>
<li class="item"><img src="./3平板5.webp" alt="" /></li>
<li class="item"><img src="./4windows11.webp" alt="" /></li>
<li class="item"><img src="./5shiduqi.webp" alt="" /></li>
</ul>
<ul class="pointList">
<li class="point active" data-index="0"></li>
<li class="point" data-index="1"></li>
<li class="point" data-index="2"></li>
<li class="point" data-index="3"></li>
<li class="point" data-index="4"></li>
</ul>
<button type="button" class="btn" id="shang"><</button>
<button type="button" class="btn" id="xia">></button>
</div>
</body>
<script>
var items = document.getElementsByClassName("item");
var points = document.getElementsByClassName("point");
var shangbtn = document.getElementById("shang");
var xiabtn = document.getElementById("xia");
var time = 0;
var index = 0;
var clearActuve = function () {
for (var i = 0; i < items.length; i++) {
items[i].className = "item";
}
for (var i = 0; i < points.length; i++) {
points[i].className = "point";
}
};
var goIndex = function () {
clearActuve();
points[index].className = "point active";
items[index].className = "item active";
};
var xia = function () {
if (index < 4) {
index++;
} else {
index = 0;
}
goIndex();
};
var shang = function () {
if (index == 0) {
index = 4;
} else {
index--;
}
goIndex();
};
xiabtn.addEventListener("click", function () {
xia();
});
shangbtn.addEventListener("click", function () {
shang();
});
for (var i = 0; i < points.length; i++) {
points[i].addEventListener("click", function () {
var pointIndex = this.getAttribute("data-index");
index = pointIndex;
goIndex();
time = 0;
});
}
setInterval(function () {
time++;
xia();
time = 0;
}, 3000);
</script>
</html>