Files
ClassContent/历届学生/韩想/项目开发/课程项目/轮播图/index.html
2024-09-27 02:06:13 +08:00

177 lines
3.8 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>轮播图</title>
<style>
body, ul, li{
padding: 0;
margin: 0;
}
li {
list-style: none;
}
.main {
width: 520px;
height: 280px;
border: 1px solid #000;
margin: 80px auto;
position: relative;
overflow: hidden;
}
.main .pic{
width: 1560px;
position: absolute;
transition: all ease 0.4s;
}
.main li {
float: left;
}
.main img {
width: 520px;
height: 280px;
}
.yd {
position: absolute;
z-index: 2;
left: 50%;
margin-left: -43.5px;
bottom: 12px;
}
.yd li {
width: 20px;
height: 20px;
background: #fff;
margin-right: 9px;
border-radius: 100%;
}
.active {
background: red !important;
}
.action {
position: absolute;
top: 50%;
z-index: 3;
width: 100%;
margin-top: -26.5px;
}
.left {
font-size: 38px;
color: #fff;
float: left;
cursor: pointer;
}
.right {
font-size: 38px;
color: #fff;
float: right;
cursor: pointer;
}
</style>
</head>
<body>
<div class="main">
<ul class="pic" style="left: 0px">
<li><img src="https://aecpm.alicdn.com/tfscom/TB1CaY1PFXXXXaDXVXXXXXXXXXX.png" alt=""></li>
<li><img src="https://img.alicdn.com/simba/img/TB11SZkQFP7gK0jSZFjSuw5aXXa.jpg" alt=""></li>
<li><img src="https://img.alicdn.com/imgextra/i3/2207127032772/O1CN01QoTCUp1WLcVg3hv8t_!!2207127032772.jpg" alt=""></li>
</ul>
<!-- 小圆点 -->
<ul class="yd">
<li class="active"></li>
<li></li>
<li></li>
</ul>
<div class="action">
<div class="left"><</div>
<div class="right">></div>
</div>
</div>
</body>
<script>
var Pic = document.querySelector(".pic"),
LeftAction = document.querySelector(".left"),
RightAction = document.querySelector(".right"),
Main = document.querySelector(".main"),
YdLi = document.querySelectorAll(".yd li");
var AutoPlay;
LeftAction.onclick = function () {
change(520)
}
function change(off) {
var oldLeft = parseInt(Pic.style.left);
var newLeft = oldLeft + (off);
var index = parseInt(`${(newLeft / -520)}`);
if (newLeft > 0) {
newLeft = -1040
index = 2
}
if (newLeft < -1040) {
newLeft = 0;
index = 0
}
ClearActive()
YdLi[index].className = "active";
dh(newLeft)
}
RightAction.onclick = function () {
change(-520)
}
function dh(slide) {
Pic.style.left = `${slide}px`
}
YdLi.forEach(function (v,ix){
v.onclick = function () {
ClearActive()
this.className = "active"
var left = ix * -520;
dh(left)
}
})
function ClearActive() {
YdLi.forEach(function (a,b){
a.className = ""
});
}
Main.onmouseover = function () {
clearInterval(AutoPlay)
}
Main.onmouseout = function () {
Auto()
}
function Auto() {
AutoPlay = setInterval(function (){
RightAction.onclick()
}, 2000);
}
Auto()
</script>
</html>