css源文件
#slideshow {
width: 1226px;
height: 460px;
overflow: hidden;
position: relative;
margin: 50px auto;
}
#pre {
position: absolute;
top: 210px;
left: 195px;
width: 50px;
height: 100px;
font-size: 40px;
opacity: 0.5;
cursor: pointer;
}
#next {
position: absolute;
top: 210px;
left: 1330px;
width: 50px;
height: 100px;
font-size: 40px;
opacity: 0.5;
cursor: pointer;
}
#imglist li {
position: absolute;
opacity: 1;
display: block;
}
#dotlist {
position: absolute;
bottom: 30px;
width: 100px;
right: 0px;
list-style: none;
display: flex;
}
div ul:last-child li{
width: 20px;
height: 20px;
text-align: center;
border-radius: 50%;
background-color: rgb(206, 16, 16);
opacity: 0.3;
cursor: pointer;
user-select: none;
}
.arrow1 {
font-size: 135px;
color: white;
}
Html源文件
<!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>
<link rel="stylesheet" href="css/img.css" />
</head>
<body>
<div id="slideshow">
<ul id="imglist">
<li><img src="img/banner01.jpg" alt="" /></li>
<li><img src="img/banner02.webp" alt="" /></li>
<li><img src="img/banner03.webp" alt="" /></li>
<li><img src="img/banner01.webp" alt="" /></li>
</ul>
<ul id="dotlist">
</ul>
</div>
<button id="pre"><</button>
<button id="next">></button>
<script src="js/jquery-3.5.1.js"></script>
承接上面,jquery代码
<script>
$(function () {
$("#imglist li").eq(0).stop().fadeIn();
$("#imglist li").eq(0).stop().siblings("li").fadeOut();
var index = 0;
var length = $("#imglist li").length;
var stopTime = 3000;
for (var i = 1; i <= length; i++) {
$("#dotlist").append("<li>" + i + "</li>");
}
$("#dotlist li").eq(0).css("opacity", "1");
$("#dotlist").on("click", "li", function () {
index = $(this).index();
indexImg();
});
function indexImg() {
for (var i = 0; i < length; i++) {
$("#imglist li").eq(i).stop().fadeOut();
$("#dotlist li").eq(i).css({ opacity: "0.3" });
}
$("#imglist li").eq(index).stop().fadeIn();
$("#dotlist li").eq(index).css({ opacity: "1" });
}
$("#next").click(function () {
index++;
if (index == 4) index = 0;
indexImg();
});
$("#pre").click(function () {
index--;
if (index == -1) index = 3;
indexImg();
});
interval = setInterval(function () {
index++;
if (index == length) index = 0;
indexImg();
}, stopTime);
$("#imglist li").mouseover(function () {
clearInterval(interval);
});
$("#imglist li").mouseout(function () {
interval = setInterval(function () {
index++;
if (index == length) index = 0;
indexImg();
}, stopTime);
});
</script>
</body>
</html>