图片自动轮播(一)---2017-04-05
一张一换,其他的都隐藏着:
第一种:利用td表格:
</head>
<style>
*{
margin:0px;
padding:0px;}
#datu
{
width:200px;
height:100px;
border: 1px solid #000;
overflow:hidden;
position:relative;
margin: 50px 50px;
}
#aa{
position:relative;
top:0px;
left:0px;
transition:0.5s;}
</style>
<body>
<div id="datu">
<table cellpadding="0" cellspacing="0" id="aa">
<tr height="100">
<td><img src="3.jpg" width="200px" height="100px"/></td>
<td><img src="4.jpg" width="200px" height="100px" /></td>
<td><img src="5.jpg" width="200px" height="100px" /></td>
<td><img src="6.jpg" width="200px" height="100px" /></td>
<td><img src="7.jpg" width="200px" height="100px" /></td>
</tr>
</table>
</div>
</body>
</html>
<script>
document.getElementById("aa").style.left="0px";
function shao()
{
var aa = parseInt(document.getElementById("aa").style.left);
if(aa>-800)
{
document.getElementById("aa").style.left=(aa-200)+"px"
}
else
{
document.getElementById("aa").style.left="0px"
}bb = window.setTimeout("shao()",1000);
}bb = window.setTimeout("shao()",1000);
</script>
第二种:利用display属性
<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="img/11.jpg" width="1000" height="250" />
<img src="img/22.png" width="1000" height="250" style="display:none" />
<img src="img/33.png" width="1000" height="250" style="display:none" />
<img src="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>