Files
ClassContent/历届学生/刘合群/2019-07-22/轮播图.html
2024-09-27 02:06:13 +08:00

185 lines
4.1 KiB
HTML

<!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> js 轮播图 </title>
<style>
body,ul,li {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.main {
width: 520px;
height: 280px;
margin: 0 auto;
border: 1px solid #000000;
overflow: hidden;
position: relative;
}
.item {
width: 1560px;
overflow: hidden;
width: 1560px;
overflow: hidden;
position: absolute;
transition: all 0.3s;
}
.item li {
float: left;
}
.action {
width: 100%;
position: absolute;
top: 50%;
margin-top: -25px;
padding: 0 10px;
box-sizing: border-box;
}
.action div{
width: 50px;
height: 50px;
text-align: center;
line-height: 43px;
color: #ffffffb0;
font-size: 33px;
background: #00000070;
border-radius: 100%;
display: inline-block;
}
.action-right {
float: right;
}
.yuandian {
position: absolute;
left: 50%;
margin-left: -30px;
height: 40px;
bottom: 0;
z-index: 9;
}
.yuandian li {
float: left;
width: 10px;
height: 10px;
border-radius: 100%;
margin-right: 10px;
background: #fff;
}
.active {
background: red !important;
}
</style>
</head>
<body>
<div class="main">
<ul class="item" style="left:0px">
<li>
<img src="https://img.alicdn.com/simba/img/TB1Z2nmeW5s3KVjSZFNSuwD3FXa.jpg" alt="">
</li>
<li>
<img src="https://img.alicdn.com/simba/img/TB1IvbNaEY1gK0jSZFCSuwwqXXa.jpg" alt="">
</li>
<li>
<img src="https://img.alicdn.com/tfs/TB1YKULarH1gK0jSZFwXXc7aXXa-520-280.jpg_q90_.webp" alt="">
</li>
</ul>
<ul class="yuandian">
<li class="active"></li>
<li></li>
<li></li>
</ul>
<div class="action">
<div class="action-left"> < </div>
<div class="action-right"> > </div>
</div>
</div>
<script>
var timer, currenIndex = 0;
document.querySelector(".action-right").onclick = function () {
var style = parseInt(document.querySelector(".item").style.left)
var newLeft = (style - 520) + 'px';
if(currenIndex>=2) {
currenIndex = -1
}
currenIndex++
liandongyuand()
if(style <= -1040) {
document.querySelector(".item").style.left = '0px'
} else {
document.querySelector(".item").style.left = newLeft
}
}
document.querySelector(".action-left").onclick = function () {
var style = parseInt(document.querySelector(".item").style.left)
var newLeft = (style + 520) + 'px';
if(currenIndex<=0) {
currenIndex = 3
}
currenIndex--
liandongyuand()
if(style>=0) {
document.querySelector(".item").style.left = '-1040px'
} else {
document.querySelector(".item").style.left = newLeft
}
}
document.querySelector(".main").onmousemove = function () {
clearInterval(timer)
}
document.querySelector(".main").onmouseout = function () {
autoPlay()
}
function liandongyuand () {
var li = document.querySelectorAll(".yuandian li");
li.forEach(function(val) {
val.className = ""
})
li[currenIndex].className = "active"
}
function ydClick () {
var li = document.querySelectorAll(".yuandian li");
li.forEach(function(val,index) {
val.onclick = function () {
li.forEach(function(val) {
val.className = ""
})
val.className = "active"
offsetLeft(index)
}
})
}
function offsetLeft(index) {
document.querySelector(".item").style.left = (index * -520 ) + 'px'
}
function autoPlay () {
timer = setInterval(function() {
document.querySelector(".action-right").onclick()
}, 1000)
}
ydClick()
autoPlay()
</script>
</body>
</html>