<!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="">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
ul,
ol {
list-style: none;
}
.box {
width: 800px;
height: 500px;
margin: 0 auto;
position: relative;
overflow: hidden;
box-shadow: 2px 2px 15px #333;
}
ul {
height: 100%;
position: absolute;
display: flex;
}
li,
li>img {
width: 800px;
height: 100%;
}
button {
width: 70px;
height: 50px;
position: absolute;
z-index: 10;
top: calc(50% - 25px);
font-size: 16px;
opacity: 0;
transition: all .5s;
}
.right {
right: 0;
}
.box:hover button {
opacity: 1;
}
ol {
width: 120px;
margin: 0 auto;
position: absolute;
bottom: 20px;
left: calc(50% - 60px);
display: flex;
justify-content: space-between;
}
ol li {
width: 10px;
height: 10px;
background-color: gray;
border-radius: 5px;
}
.ac {
width: 25px;
background-color: blue;
transition: all 1s;
}
</style>
</head>
<body>
<div class="box">
<ul>
<li><img class="activeImg" src="../../../01-JQuary练习/jq学习/images/冬裙.jpg" alt=""></li>
<li><img src="../../../01-JQuary练习/jq学习/images/呢大衣.jpg" alt=""></li>
<li><img src="../../../01-JQuary练习/jq学习/images/围巾.jpg" alt=""></li>
<li><img src="../../../01-JQuary练习/jq学习/images/女包.jpg" alt=""></li>
<li><img src="../../../01-JQuary练习/jq学习/images/棉服.jpg" alt=""></li>
<li><img class="activeImg" src="../../../01-JQuary练习/jq学习/images/冬裙.jpg" alt=""></li>
</ul>
<button class="left">left</button>
<button class="right">right</button>
<ol class="dots">
<li class="ac"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
</div>
<script>
let width = $("img").width();
console.log(width);
let index = 0;
let imglen = $("img").length - 1;
console.log(imglen);
let timer;
let flag = false;
function dotCHange(index) {
$("ol li").removeClass("ac");
$("ol li").eq(index).addClass("ac")
}
function play() {
if (flag) {
return;
}
flag = true;
index++;
if (index == imglen) {
$("ul").animate({
left: -index * width
}, 1000, function () {
$("ul").css("left", 0)
flag = false;
})
index = 0
} else {
$("ul").animate({
left: -index * width
}, 1000, function () {
flag = false;
})
}
dotCHange(index)
}
timer = setInterval(play, 1000)
$(".box").hover(function () {
clearInterval(timer);
timer = null;
}, function () {
timer = setInterval(play, 1000)
})
$("ol li").click(function () {
let dotidx = $(this).index();
index = dotidx;
console.log(dotidx);
$("ul").stop().animate({
left: -dotidx * width
}, 1000)
dotCHange(dotidx)
})
$(".right").on("click", function () {
play();
})
$(".left").on("click", function () {
if (flag) {
return;
}
flag = true;
index--;
if (index < 0) {
index = imglen - 1;
$("ul").css("left", -imglen * width);
$("ul").animate({
left: -index * width
}, 1000, function () {
flag = false
})
} else {
$("ul").animate({
left: -index * width
}, 1000, function () {
flag = false
})
}
dotCHange(index)
})
</script>
</body>
</html>