首页 > 代码库 > 图片轮播
图片轮播
<style type="text/css">
*{ margin:0px auto; padding:0px; font-family:微软雅黑; font-size:14px;}
</style>
</head>
<body>
<div style="width:1000px; height:250px; margin-top:30px">
<img src="http://www.mamicode.com/img/11.jpg" width="1000" height="250" />
<img src="http://www.mamicode.com/img/22.png" width="1000" height="250" style="display:none" />
<img src="http://www.mamicode.com/img/33.png" width="1000" height="250" style="display:none" />
<img src="http://www.mamicode.com/img/44.png" width="1000" height="250" style="display:none" />
</div>
</body>
<script type="text/javascript">
window.setInterval("Huan()",2000);
//找到图片的最大索引
var n = document.getElementsByTagName("img").length-1;
//存当前图片的索引
var d =0;
//换图
function Huan()
{
//找到所有图片
var attr = document.getElementsByTagName("img");
//当前索引加1
d++;
//判断索引是否超出范围
if(d>n)
{
d = 0;
}
//换图
//让所有隐藏
for(var i=0;i<=n;i++)
{
attr[i].style.display = "none";
}
//让该索引的显示
attr[d].style.display = "block";
}
</script>
</html>
图片轮播