<!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>
*{
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
#banner{
width: 800px;
height: 400px;
border: 2px solid #999;
overflow: hidden;
position: relative;
}
#imglist{
width: 4000px;
height: 400px;
}
#imglist img{
width: 800px;
height: 400px;
}
#imglist li{
float: left;
}
.prev{
background-color: #666;
width: 30px;
height: 40px;
color: #fff;
text-align: center;
line-height: 40px;
position: absolute;
left: 5px;
top: 45%;
cursor: pointer;
}
.next{
background-color: #666;
width: 30px;
height: 40px;
color: #fff;
text-align: center;
line-height: 40px;
position: absolute;
right: 5px;
top: 45%;
cursor: pointer;
}
#icolist{
position: absolute;
right: 10px;
bottom: 10px;
}
#icolist li{
width: 30px;
height: 30px;
border-radius: 50%;
background-color: #666;
text-align: center;
line-height: 30px;
color: #fff;
float: left;
margin-left: 5px;
}
</style>
</head>
<body>
<div id="banner">
<ul id="imglist">
<li><img src="./imgs/tu1.webp" alt=""></li>
<li><img src="./imgs/tu2.webp" alt=""></li>
<li><img src="./imgs/tu3.webp" alt=""></li>
<li><img src="./imgs/tu4.webp" alt=""></li>
<li><img src="./imgs/tu1.webp" alt=""></li>
</ul>
<ul id="icolist">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div class="prev"> 》</div>
<div class="next">《</div>
</div>
<script>
var eprve = document.querySelector(".prev");
var enext = document.querySelector(".next");
var esico = document.getElementById("icolist").getElementsByTagName("li");
var eicolist = document.querySelector("#icolist");
var eimglist = document.querySelector("#imglist");
var left = 0;
var timer;
run();
function run(){
if (left <= -3000) {
left=0;
}
var m = Math.floor(-left / 800);
imglist.style.marginLeft = left + "px";
var n = (left % 800 ==0) ? n=1200 : n = 10;
left-= 10;
timer = setTimeout(run,n);
icochange(m);
}
//做一个图片定位函数
function imgchange(n) {
let lt = -(n * 800);
imglist.style.marginLeft = lt + "px";
left = lt;
}
eprve.onclick = function(){
//试着定位第一张
let prevgo = Math.floor(-left /800) - 1;
if (prevgo == -1) {
prevgo = 3;
}
imgchange(prevgo);
}
enext.onclick = function(){
//定位做后一张
let nextgo = Math.floor(-left /800) + 1;
if (nextgo == 4) {
nextgo = 0;
}
imgchange(nextgo);
}
function icochange(m) {
for (let index = 0; index < esico.length; index++) {
esico[index].style.backgroundColor = "";
if (m <esico.length) {
esico[m].style.backgroundColor = "red";
}
}
}
eicolist.onclick = function(){
var tg = event.target;
let ico = tg.innerHTML -1;
imgchange(ico);
icochange(ico);
}
eimglist.onmouseover = function(){
clearTimeout(timer);
}
eimglist.onmouseout = function(){
run();
}
</script>
</body>
</html>
拿走别客气 图片换一下就行