轮播图功能实现
自动进行图片轮播功能
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box {
width: 800px;
height: 600px;
border:2px solid #000;
margin: 100px auto;
position: relative;
overflow: hidden;
}
li{
list-style:none;
}
ul {
width:4000px;
height: 600px;
position: relative;
}
ul li {
width: 800px;
height: 600px;
float: left;
}
ul li img{
width:800px;
height: 600px;
}
ol {
width: 200px;
height: 20px;
position: absolute;
bottom: 40px;
right: 40px;
}
ol li {
width: 20px;
height: 20px;
background: #000;
float: left;
border-radius: 50%;
}
ol li+li {
margin-left: 10px;
}
ol li.active{
color: red;
}
.btn {
width: 40px;
height: 60px;
font-size: 40px;
text-align: center;
line-height: 60px;
color: #fff;
position: absolute;
top: 50%;
margin-top: -30px;
background-color: rgba(0,0,0,0.5);
}
.left{
left:0
}
.right{
right:0
}
</style>
</head>
<body>
<div class="box">
<ul>
<li><img src="images/1.jpeg" alt=""></li>
<li><img src="images/2.jpeg" alt=""></li>
<li><img src="images/3.jpeg" alt=""></li>
<li><img src="images/4.jpeg" alt=""></li>
<li><img src="images/5.jpeg" alt=""></li>
</ul>
<ol>
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
<p class="btn left"><</p>
<p class="btn right">></p>
</div>
<script src="utils.js"></script>
<script>
const ul = my$('ul')[0]
const ulis = my$('li',ul)
const liWidth = ulis[0].offsetWidth
let currentIndex = 0
let nextIndex = 1
setInterval(() => {
animate(ul,{left:-liWidth * nextIndex},1000)
currentIndex = nextIndex
nextIndex++
if (nextIndex>=5){
nextIndex=0
}
}, 2000);
</script>
</body>
</html>