<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>焦点轮播图</title>
<style>
*{margin:0; padding:0}
img{border:0; vertical-align:bottom}
a{text-decoration:none}
#container{width:600px; height:400px; border:1px solid #333; overflow:hidden; position:relative; margin:30px auto}
#list{width:4200px; height:400px; position:absolute}
#list img{float:left}
#buttons{ height:10px; width:100px; position:absolute; bottom:20px; left:250px}
#buttons span{ width:8px; height:8px; float:left; border-radius:5px; border:1px #fff solid; margin:0 10px 0 0}
#buttons .on{background:#fff}
.arrow{cursor:pointer; line-height:39px; text-align:center; font-size:36px; position:absolute; font-weight:bold; top:170px; color:#fff; display:block; width:35px; border-radius:5px; display:none}
.arrow:hover{background-color:RGBA(0,0,0,.5)}
#container:hover .arrow{display:block;}
#prev{left:20px}
#next{right:20px}
</style>
<script>
window.onload=function(){
var container=document.getElementById("container");
var list=document.getElementById("list");
var buttons=document.getElementById("buttons").getElementsByTagName("span");
var prev=document.getElementById("prev");
var next=document.getElementById("next");
var index=1;//小圆点位置
var animated=false;//判断是否在运行animat函数
var timer;
//改变小圆点的当前颜色
function showButton(){
for(var i=0; i<buttons.length; i++){
buttons[i].className='';
}
buttons[index-1].className='on';
}
//切换图片(判断第一张和最后一张特殊的位置),offset每次切换的数值
function animate(offset){//
animated=true;
var newLeft=parseInt(list.style.left)+offset;
var time=300;//位移总时间
var interval=10;//位移间隔时间
var speed=offset/ (time/interval);//每次位移量(负数:向左; 正数:向右)
function go(){
if((speed<0 && parseInt(list.style.left)>newLeft) || (speed>0 && parseInt(list.style.left) < newLeft)){
list.style.left=parseInt(list.style.left)+speed+'px';
setTimeout(go,interval);//调自己的函数,即递归
}
else{
animated = false;
list.style.left = newLeft+'px';
if(newLeft > -600){//img1的位置
list.style.left = -3000 + 'px';
}
if(newLeft < -3000){//img2的位置
list.style.left = -600 + 'px';
}
}
}
go();
}
function play(){
timer=setInterval(function(){
next.onclick();
},3000);
}
function stop(){
clearInterval(timer);
}
play();
container.onmouseover = stop;
container.onmouseout = play;
next.onclick=function(){
if(index==5){
index=1;
}else{
index+=1;
}
showButton();
if(!animated){
animate(-600);
}
}
prev.onclick=function(){
if(index==1){
index=5;
}else{
index-=1;
}
showButton();
if(!animated){
animate(600);
}
}
for(var i=0; i<buttons.length; i++){
buttons[i].onclick=function(){
if(this.className=='on'){//当点击当前图片的小图标,return退出当前函数
return;
}
//获取当前点击时属性里的Index值
var myIndex=parseInt(this.getAttribute('index'));//获取自定义属性或dom自带属性
var offset=-600*(myIndex-index);
if(!animated){
animate(offset);
}
index=myIndex;
showButton();
}
}
}
</script>
</head>
<body>
<div id="container">
<div id="list" style="left:-600px">
<img src="images/5.jpg" alt=""/>
<img 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=""/>
<img src="images/1.jpg" alt=""/>
</div>
<div id="buttons">
<span index="1" class="on"></span>
<span index="2" ></span>
<span index="3" ></span>
<span index="4" ></span>
<span index="5" ></span>
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
</body>
</html>