DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动画的轮播title>
<style>
*{
margin:0;
padding:0;
}
.banner{
width: 900px;
height: 460px;
margin:10px auto 0;
position: relative;
border:1px solid #000;
overflow: hidden;
}
.img_box{
position: absolute;
width: 605%;
height: 460px;
top:0;
left:0;
}
.img_box img{
width: 900px;
height: 460px;
float: left;
}
.icon{
position: absolute;
bottom: 5px;
left: 50%;
width: 250px;
margin-left: -125px;
z-index: 1;
}
.icon span{
float: left;;
width: 30px;
height: 30px;
line-height: 30px;
text-align:center;
margin:10px;
background-color: #eee;
cursor: pointer;
}
.icon .active,
.icon span:hover{
background-color: orange;
color:#fff;
}
.left,
.right{
position: absolute;
top:50%;
height: 50px;
line-height: 40px;
text-align: center;
font-size: 40px;
color:#fff;
background-color: #eee;
cursor: pointer;
z-index:1;
}
.left:hover,
.right:hover{
background-color: #aaa;
}
.left{
left:10px;
}
.right{
right:10px;
}
style>
head>
<body>
<div class='banner'>
<div class="img_box">
<img class="select" src="images/1.jpg" alt="">
<img src="images/2.jpg" alt="">
<img src="images/3.jpg" alt="">
<img src="images/4.jpg" alt="">
<img src="images/5.jpg" alt="">
div>
<div class="icon">
div>
<div class="left"> < div>
<div class="right"> > div>
div>
<script>
var ban = document.querySelector(".banner");
var imgs = document.querySelectorAll(".banner img");
var img_box = document.querySelector(".img_box");
var icon = document.querySelector(".icon");
var left = document.querySelector(".left");
var right = document.querySelector(".right");
ban.addEventListener('mouseenter',function(){
right.style.display = "block";
left.style.display = "block";
clearInterval(timer);
timer = null;
});
ban.addEventListener('mouseleave',function(){
right.style.display = "none";
left.style.display = "none";
timer = setInterval(function(){
right.click();
},2000);
});
for(var i = 0;i<img_box.children.length;i++){
var span = document.createElement("span");
span.setAttribute('index',i);
icon.appendChild(span);
span.innerHTML=i+1;
span.onclick = function (){
document.querySelector(".active").className = "";
this.className = "active";
var index = this.getAttribute("index");
num = index;
circle = index;
animate(img_box,- index * img_box.children[0].offsetWidth);
}
}
var spans = document.querySelectorAll(".icon span");
spans[0].className = "active";
var first = img_box.children[0].cloneNode(true);
img_box.appendChild(first);
var num = 0;
var circle = 0;
console.log(num,circle);
right.addEventListener('click',function (){
if(num == img_box.children.length-1){
img_box.style.left =0;
num =0;
}
num++;
animate(img_box,-num*img_box.children[0].offsetWidth);
circle++;
if(circle == icon.children.length){
circle = 0;
}
document.querySelector(".active").className ="";
icon.children[circle].className ="active";
})
left.addEventListener('click',function (){
if(num == 0){
num = img_box.children.length -1;
img_box.style.left=num * img_box.children[0].offsetWidth +"px";
}
num--;
animate(img_box,-num*img_box.children[0].offsetWidth);
circle--;
console.log('circle',circle);
console.log(icon.children.length);
circle = circle < 0 ? icon.children.length -1 :circle;
document.querySelector(".active").className ="";
icon.children[circle].className ="active";
})
var timer = setInterval(function (){
right.click();
},2000);
function animate(element,position){
clearInterval(element.timer);
element.timer = setInterval(function (){
var current = element.offsetLeft;
var step = (position - current)/10;
step = step > 0 ? Math.ceil(step) : Math.floor(step) ;
if(Math.abs(current - position) > step){
element.style.left = current + step +"px";
}else{
element.style.left = position + "px";
clearInterval(element.timer);
}
},20)
}
script>
body>
html>