【js特效】图片循环滚动代码

1.效果图如下:

【js特效】图片循环滚动代码_第1张图片


2.html代码:

<!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>图片滚屏效果 - www.miaov.com - 妙味课堂</title>
<link href="miaov_style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="miaov.js">
</script>
</head>
<body>
<div class="control">
	<label id="chk_pause"><input type="checkbox" checked="checked">间隔停顿</label>
	<select id="pause_time">
		<option value="100">短</option>
		<option value="1000" selected="selected">中</option>
		<option value="3000">长</option>
	</select>

	滚动速度:
	<select id="sel_speed">
		<option value="2">慢</option>
		<option value="5">中</option>
		<option value="10">快</option>
	</select>
</div>

<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>
        </ul>
    </div>
</div>
</body>
</html>

3.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; }

4.js代码:

var g_bMoveLeft=true;
var g_oTimer=null;
var g_oTimerOut=null;

var g_iSpeed=3;

window.onload=function ()
{
	var oDiv=document.getElementById('roll');
	var oUl=oDiv.getElementsByTagName('ul')[0];
	var aLi=oUl.getElementsByTagName('li');//获取<ul>标签中所有的<li>元素
	var aA=oDiv.getElementsByTagName('a');//获取<ul>标签中所有的<a>元素
	var i=0;
	var str=oUl.innerHTML+oUl.innerHTML;//两组 <li>元素
	oUl.innerHTML=str;
	oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';//两组<li>标签的总宽度
	for(i=0;i<aLi.length;i++){
		aLi[i].onmouseover=function (){
			stopMove();//鼠标移上去时停止移动
		};
		
		aLi[i].onmouseout=function (){
			startMove(g_bMoveLeft);//开始向左移动
		};
	}
	
	aA[0].onmouseover=function (){
		startMove(true);
	};
	
	aA[1].onmouseover=function (){
		startMove(false);
	};
	
	startMove(true);
};

function startMove(bLeft){
	g_bMoveLeft=bLeft;	
	if(g_oTimer){
		clearInterval(g_oTimer);
	}
	g_oTimer=setInterval(doMove, 30);
}

function stopMove(){
	clearInterval(g_oTimer);
	g_oTimer=null;
}

function doMove(){
	var oDiv=document.getElementById('roll');
	var oUl=oDiv.getElementsByTagName('ul')[0];
	var aLi=oUl.getElementsByTagName('li');
	var l=oUl.offsetLeft;
	if(g_bMoveLeft){
		l-=g_iSpeed;
		if(l<=-oUl.offsetWidth/2){
			l+=oUl.offsetWidth/2;
		}
	}else{
		l+=g_iSpeed;
		if(l>=0){
			l-=oUl.offsetWidth/2;
		}
	}
	oUl.style.left=l+'px';
}




你可能感兴趣的:(JavaScript,function,XHTML,url,Class,stylesheet)