javascript实现图片无缝滚动

代码如下;

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>图片滚屏效果</title>
<link href="miaov_style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
window.οnlοad=function(){
	var oDiv=document.getElementById('roll');
	var oUl=oDiv.getElementsByTagName('ul')[0];
	var aLi=oUl.getElementsByTagName('li');
	var aBtn=oDiv.getElementsByTagName('a');
	
	var iSpeed=-5;
	var timer=null;
	oUl.innerHTML+=oUl.innerHTML;
	
	oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';
	timer = setInterval(function(){
		oUl.style.left=oUl.offsetLeft+iSpeed+'px';
		if(oUl.offsetLeft<-oUl.offsetWidth/2){
			oUl.style.left='0px';
		}else if(oUl.offsetLeft>0){
			oUl.style.left=-oUl.offsetWidth/2+'px';
		}
	},30);
	
	aBtn[0].οnmοuseοver=function(){
		iSpeed=-5;
	};
	aBtn[1].οnmοuseοver=function(){
		iSpeed=5;
	};
	
	oUl.οnmοuseοver=function(){
		clearInterval(timer);
	};
	oUl.οnmοuseοut=function(){
		timer = setInterval(function(){
		oUl.style.left=oUl.offsetLeft+iSpeed+'px';
		if(oUl.offsetLeft<-oUl.offsetWidth/2){
			oUl.style.left='0px';
		}else if(oUl.offsetLeft>0){
			oUl.style.left=-oUl.offsetWidth/2+'px';
		}
	},30);
	};
};
</script>
</head>
<body>

<div class="roll" id="roll">
    <a href="javascript:void(0);" class="btn_left"></a>
    <a href="javascript:void(0);" class="btn_right"></a>
    <div class="wrap">
        <ul>
            <li><a href="http://www.miaov.com/"><img src="images/1.jpg" /></a></li>
            <li><a href="http://www.miaov.com/"><img src="images/2.jpg" /></a></li>
            <li><a href="http://www.miaov.com/"><img src="images/3.jpg" /></a></li>
            <li><a href="http://www.miaov.com/"><img src="images/4.jpg" /></a></li>
			<li><a href="http://www.miaov.com/"><img src="images/1.jpg" /></a></li>
            <li><a href="http://www.miaov.com/"><img src="images/2.jpg" /></a></li>
            <li><a href="http://www.miaov.com/"><img src="images/3.jpg" /></a></li>
            <li><a href="http://www.miaov.com/"><img src="images/4.jpg" /></a></li>
			
        </ul>
    </div>
</div>
</body>
</html>
其中css文件如下:

* { padding: 0; margin: 0; }
li { list-style: none; }
img { border: 0; }

.roll { width: 698px; height: 108px; margin: 50px auto 0; position: relative; }
.btn_left { display: block; width: 68px; height: 68px; background: url(images/btn.jpg) no-repeat -70px -69px; position: absolute; top: 20px; left: 1px; z-index: 1; }
.btn_left:hover { background: url(images/btn.jpg) no-repeat -70px 0; }
.btn_right { display: block; width: 68px; height: 68px; background: url(images/btn.jpg) no-repeat 1px -69px; position: absolute; top: 20px; right: 0; z-index: 1; }
.btn_right:hover { background: url(images/btn.jpg) no-repeat 1px 0; }
.roll .wrap { width: 546px; height: 108px; margin: 0 auto; position: relative; overflow: hidden; }
.roll ul{ position: absolute; top: 0; left: 0; }
.roll li { float: left; width: 182px; height: 108px; text-align: center; }
.roll li a:hover { position: relative; top: 2px; }

.control { border-bottom: 1px solid #ccc; background: #eee; text-align: center; padding: 20px 0; display: none; }

效果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值