js控制图片定时切换不限制数量

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>

<div id="ImageDiv" style="overflow:hidden;height:253px;width:410px;">
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">11</p>
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">22</p>
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">33</p>
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">44</p>
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">55</p>
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">66</p>
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">77</p>
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">88</p>
<p style="display:none;"><img src="http://js.alixixi.com/img/201108/1.jpg">99</p>
</div>

<script>
var speed=3000;
var imgObj=document.getElementById("ImageDiv").getElementsByTagName("p");
var imgNum=imgObj.length;
var mm=0;
var ImgTurn=function()
{
    if(mm==imgNum)
 {
    mm=0;
 }
 imgObj[mm].style.display="block";
 if(mm==0)
 {
     imgObj[imgNum-1].style.display="none";
 }
 else
 {
     imgObj[mm-1].style.display="none";
 }
 mm++;
}

var myTurn=setInterval(ImgTurn,speed);

//鼠标移上时清除定时器达到滚动停止的目的
document.getElementById("ImageDiv").onmouseover=function() {clearInterval(myTurn);}
//鼠标移开时重设定时器
document.getElementById("ImageDiv").onmouseout=function(){myTurn=setInterval(ImgTurn,speed)};
</script>
</body>
</html>

 

posted @ 2013-05-01 22:55  javawebsoa  Views(174)  Comments(0Edit  收藏  举报