图片自动轮播(一)---2017-04-05

本文介绍了两种网页中实现图片轮播的技术方案:一种是通过调整表格单元格的位置来达到轮播效果;另一种则是利用CSS的display属性进行图片切换。这两种方法均适用于网页开发中的图片展示需求。

 

一张一换,其他的都隐藏着:

第一种:利用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>

转载于:https://www.cnblogs.com/chenguanai/p/6670358.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值