<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#box{width: 590px;height: 470px;margin:50px auto;position: relative;}
#cont div{display: none;}
#cont .sel{display: block;}
#box1{position: absolute;top:50%;margin-top: -16px;width: 100%;}
.al{float: left;width: 28px;height: 38px;background: rgb(0,0,0,0.5);padding: 2px}
.ar{float: right;width: 28px;height: 38px;background: rgb(0,0,0,0.5);padding: 2px;}
.oul{position: absolute;bottom: 15px;left: 50%;transform: translateX(-50%);}
li{list-style: none;width: 10px;height: 10px;border: 1px solid gray;border-radius: 50%;float: left;margin-right: 10px;}
.oul .oli{background: #fff;}
</style>
</head>
<body>
<div id="box">
<!-- 轮播图 -->
<div id="cont">
<div class="sel"><img src="../images/l1.jpg"></div>
<div><img src="../images/l2.jpg"></div>
<div><img src="../images/l3.jpg"></div>
<div><img src="../images/l4.jpg"></div>
</div>
<!-- 指示器 -->
<ol class="oul">
<li class="oli"></li>
<li></li>
<li></li>
<li></li>
</ol>
<!-- 箭头 -->
<div id='box1'>
<img src="../images/left.png" class="arrow al">
<img src="../images/right.png" class="arrow ar">
</div>
</div>
<script type="text/javascript">
var os=document.getElementById('cont').getElementsByTagName('div');
var oc=document.getElementById('box');//轮播区
var arrows=document.getElementsByClassName('arrow');
var lis=document.getElementsByClassName('oul')[0].getElementsByTagName('li');
var index=0;//当前显示的轮播索引
var len=os.length;//轮播图个数
var t;
//1.自动轮播
t=setInterval(moveNext,2000);
//2.鼠标进入,停止轮播
oc.onmouseenter=function(){
oc.style.cursor='pointer';
clearInterval(t);
}
//3.鼠标离开,继续
oc.onmouseleave=function(){
t=setInterval(moveNext,2000);
}
//4.箭头轮播
for(var i=0;i<arrows.length;i++){
arrows[i].onmouseenter=function(){
this.style.background='rgb(0,0,0,0.6)';
};
arrows[i].onmouseleave=function(){
this.style.background='rgb(0,0,0,0.5)';
}
}
//左箭头
arrows[0].onclick=function(){
movePre();
}
//右箭头
arrows[1].onclick=function(){
moveNext();
}
//5.指示器
for(var j=0;j<len;j++){
lis[j]._index=j;//为每一个li新增属性,赋值为当前索引
lis[j].onclick=function(){
//当前显示的修改
lis[index].className='';
os[index].className='';
//被点击的改变
this.className='oli';
os[this._index].className='sel';
index=this._index;//修改index值
}
}
//显示下一张
function moveNext(){
//当前显示的置为不显示
os[index].className='';
lis[index].className='';
index++;//索引增加
//最后一张
if(index==len){
index=0;
}
//下一张显示
os[index].className='sel';
lis[index].className='oli';
}
//显示上一张
function movePre(){
//当前显示的置为不显示
os[index].className='';
lis[index].className='';
index--;
//把第一张调整为最后一张
if(index==-1){
index=len-1;
}
//上一张显示
os[index].className='sel';
// lis[index].className='oli';
}
</script>
</body>
</html>