页面布局:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #666;
}
header {
width: 100%;
height: 200px;
/* background-color: pink; */
}
ul,
ol {
list-style: none;
}
.clearfix:after {
/*伪元素是行内元素 正常浏览器清除浮动方法*/
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
header img {
width: 70%;
height: 200px;
display: block;
margin: 0 auto;
}
.inner {
width: 500px;
height: 300px;
background-color: pink;
overflow: hidden;
margin: 10px auto;
position: relative;
}
.inner ul {
width: 1000%;
position: absolute;
top: 0;
left: 0;
}
.lunbo li {
float: left;
}
.inner li img {
width: 500px;
height: 300px;
}
.circle {
height: 15px;
width: 100px;
position: absolute;
top: 90%;
left: 50%;
cursor: pointer;
transform: translate(-50%, -50%);
}
.circle li {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #fff;
float: left;
margin-left: 3px;
}
.box_r,
.box_l {
position: absolute;
display: none;
height: 30px;
width: 30px;
background-color: #CCC;
text-align: center;
line-height: 30px;
cursor: pointer;
}
.box_l {
top: 50%;
left: 5px;
transform: translateY(-50%);
}
.box_r {
top: 50%;
right: 5px;
transform: translateY(-50%);
}
.circle .active {
background-color: chocolate;
}
</style>
</head>
<body>
<header>
<img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3831263563,3611204861&fm=26&gp=0.jpg" alt="">
</header>
<div class="inner">
<ul class="lunbo ">
<li><img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2613286700,2153536478&fm=26&gp=0.jpg" alt=""></li>
<li><img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3326645420,1548960890&fm=26&gp=0.jpg" alt=""></li>
<li><img src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1570190499,3408240602&fm=26&gp=0.jpg" alt=""></li>
</ul>
<ol class="circle">
</ol>
<div class="box_l">
<span> < </span>
</div>
<div class="box_r">
<span>></span>
</div>
</div>
</body>
<script src="./animition.js"></script>
</html>
js代码:
window.addEventListener('load', function() {
function animition(obj, target, callback) {
clearInterval(obj.timer)
obj.timer = setInterval(function() {
var step = (target - obj.offsetLeft) / 10
step = step > 0 ? Math.ceil(step) : Math.floor(step)
if (obj.offsetLeft == target) {
clearInterval(obj.timer)
callback && callback()
}
obj.style.left = obj.offsetLeft + step + 'px'
}, 15)
}
var inner = document.querySelector('.inner')
var box_l = document.querySelector('.box_l')
var box_r = document.querySelector('.box_r')
var lunbo = document.querySelector('.lunbo')
var circle = document.querySelector('.circle')
var ulWidth = lunbo.querySelector('li').offsetWidth
inner.addEventListener('mouseenter', function() {
box_l.style.display = 'block'
box_r.style.display = 'block'
clearInterval(timer)
timer = null
})
inner.addEventListener('mouseleave', function() {
box_l.style.display = 'none'
box_r.style.display = 'none'
timer = setInterval(function() {
box_r.click()
}, 1500)
})
for (var i = 0; i < lunbo.children.length; i++) {
var li = document.createElement('li')
li.setAttribute('index', i)
li.addEventListener('click', function() {
for (var i = 0; i < circle.children.length; i++) {
circle.children[i].className = ''
}
var index = this.getAttribute('index')
num = index
num1 = index
this.className = 'active'
animition(lunbo, -index * ulWidth)
})
circle.appendChild(li)
}
circle.children[0].className = 'active'
var first = lunbo.children[0].cloneNode(true)
lunbo.appendChild(first)
var num = 0
var num1 = 0
var flag = true
box_r.addEventListener('click', function() {
if (flag) {
flag = false
if (num == lunbo.children.length - 1) {
lunbo.style.left = 0
num = 0
}
num++
animition(lunbo, -num * ulWidth, () => {
flag = true
})
num1++
if (num1 == circle.children.length) {
num1 = 0
}
change()
}
})
box_l.addEventListener('click', function() {
if (flag) {
flag = false
if (num == 0) {
num = lunbo.children.length - 1
lunbo.style.left = -num * ulWidth + 'px'
}
num--
animition(lunbo, -num * ulWidth, () => {
flag = true
})
num1--
if (num1 < 0) {
num1 = circle.children.length - 1
}
change()
}
})
function change() {
for (var i = 0; i < circle.children.length; i++) {
circle.children[i].className = ''
}
circle.children[num1].className = 'active'
}
var timer = setInterval(function() {
box_r.click()
}, 1500)
})