使用html+css+javascript实现图片无缝滚动

代码如下:

<!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=utf-8" />
<title>无标题文档</title>
<style>
*{padding:0; margin:0;}
#div1 {width:712px; height:108px; position:relative; margin:100px auto; overflow:hidden;}
#div1 ul{ position:absolute; left:0; top:0;  }
#div1 ul li{ float:left; list-style:none;  }
 #div1 ul li img{width:178px; height:108px;width:178px; height:108px; }
</style>
<script>
  window.onload=function(){
	  var oBate=document.getElementById('div1');
	  var oUl=oBate.getElementsByTagName('ul')[0];
	  var oLi=oUl.getElementsByTagName('li');
	  var speed=2;
	  
	  oUl.innerHTML=oUl.innerHTML+oUl.innerHTML;
	  oUl.style.width=oLi[0].offsetWidth*oLi.length+'px';
	  function move()
	  {
			if(oUl.offsetLeft< -oUl.offsetWidth/2)
			{
				  oUl.style.left='0';
			}
			if(oUl.offsetLeft>0)
			{
				oUl.style.left=-oUl.offsetWidth/2+'px';
			}
		    oUl.style.left=oUl.offsetLeft-speed+'px'; 			   
	  }
	  var timer=setInterval( move,30);
	  oBate.onmouseover=function()
	  {
		  clearInterval(timer);   
	  };
	  oBate.onmouseout=function()
	  {
	     timer=setInterval(move,30);
	   };
	   document.getElementsByTagName('a')[0].onclick=function()
	   {
		   speed=2;
		}
		document.getElementsByTagName('a')[1].onclick=function()
	   {
		   speed=-2;
		}   
 };
</script>
</head>
<!--向左,向右调节滚动的方向-->
<body>
<a href="javascript:;">向左</a>
<a href="javascript:;">向右</a>
<div id="div1">
  <ul>
    <li><img src="img1/1.jpg" /></li>
    <li><img src="img1/2.jpg" /></li>
    <li><img src="img1/3.jpg" /></li>
    <li><img src="img1/4.jpg" /></li>
  </ul>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值