首页 > 代码库 > js小效果-轮播图
js小效果-轮播图
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
list-style: none;
}
#wrap{
width:500px;
height:260px;
margin:50px auto;
position:relative;
overflow: hidden;
border: 1px solid #000;
}
#wrap a {
width:30px;
height:30px;
line-height:30px;
background: red;
opacity: 0.3;
display: block;
text-align: center;
z-index:990;
position: absolute;
text-decoration: none;
}
#prev{left:0;
top:110px;
}
#next{right:0;
top:110px;}
#box li{
width:500px;
height:260px;
}
#box{
position: absolute;
height:1300px;
}
img{
width:500px;
height:260px;
}
#btn{
position: absolute;
bottom:10px;
text-align: center;
margin-left: 175px;
z-index: 999;
}
#btn li{
width:20px;
height:20px;
border-radius: 20px;
background: #ccc;
margin-left: 5px;
float:left;
}
#wrap #btn li.active{background:mediumvioletred;}
</style>
<script src="http://www.mamicode.com/move1.js"></script>
<script>
window.onload=function(){
var oWrap=document.getElementById(‘wrap‘);
var oPrev=document.getElementById(‘prev‘);
var oNext=document.getElementById(‘next‘);
var oBox=document.getElementById(‘box‘);
var aLi=oBox.children;
var oBtn=document.getElementById(‘btn‘);
var aBtn=oBtn.children;
var iNow=0;
for(var i=0;i<aBtn.length;i++){
aBtn[i].index=i;
aBtn[i].onclick=function(){
iNow=this.index;
tab();
}
};
function tab(){
for(var i=0;i<aBtn.length;i++){
aBtn[i].className=‘‘;
}
aBtn[iNow].className=‘active‘;
move(oBox,{top:-aLi[0].offsetHeight*iNow},{time:1000,easing:‘ease-in‘});
};
oPrev.onclick=function(){
iNow--;
if(iNow==-1)iNow=aBtn.length-1;
tab();
};
oNext.onclick=next;
function next() {
iNow++;
if (iNow == aBtn.length)iNow = 0;
tab();
};
var timer=null;
timer=setInterval(next,2000);
oWrap.onmouseover=function(){
clearInterval(timer) ;
};
oWrap.onmouseout=function(){
timer=setInterval(next,2000);
};
}
</script>
</head>
<body>
<div id="wrap">
<a href="javascript:;" id="prev">←</a>
<a href="javascript:;" id="next">→</a>
<ul id="box">
<li><img src="http://www.mamicode.com/img/b1.jpg" /></li>
<li><img src="http://www.mamicode.com/img/b2.jpg" /></li>
<li><img src="http://www.mamicode.com/img/b3.jpg" /></li>
<li><img src="http://www.mamicode.com/img/b4.jpg" /></li>
<li><img src="http://www.mamicode.com/img/b5.jpg" alt =""/></li>
</ul>
<ol id="btn">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
</div>
</body>
</html>
js小效果-轮播图