简单的左右轮播图

来源:互联网 发布:猪八戒出题软件 编辑:程序博客网 时间:2024/06/12 01:28
<script>
var box =document.getElementById("box");
var img=document.getElementById("img");
var ctl=document.getElementById("ctl");
var ul =img.children[0];
var li=ul.children;
box.onmouseover=function(){
ctl.style.display="block";
}
box.onmouseleave=function(){
ctl.style.display="none";
}
var target=0;
ctl.children[0].onclick=function(){
target+=490;
target=target>0?0:target;
move(ul,target);
}
ctl.children[1].onclick=function(){
target-=490;
if (target<-(li.length-1)*490) {
target=-(li.length-1)*490;
}
move(ul,target);
}
function move(obj,target){
clearInterval(obj.timer);
var speed = obj.offsetLeft < target ? 15 : -15;
obj.timer = setInterval(function() {
var result = target - obj.offsetLeft;  
obj.style.left = obj.offsetLeft + speed + "px";
if (Math.abs(result) <= 15) {
obj.style.left = target + "px"; 
clearInterval(obj.timer);
}
}, 10);
}


</script>