码迷,mamicode.com
首页 > 其他好文 > 详细

无缝滚动

时间:2014-12-31 11:12:54      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

效果图:

技术分享

 

html 代码:

<</SPAN>html>
<</SPAN>head lang="en">
    <</SPAN>meta charset="UTF-8">
    <</SPAN>title>无缝滚动</</SPAN>title>
    <</SPAN>script src="js/0010.js"></</SPAN>script>
    <</SPAN>link rel="stylesheet" type="text/css" href="css/0010.css" />
</</SPAN>head>
<</SPAN>body>
   <</SPAN>a href="javascript:">向左走</</SPAN>a>
   <</SPAN>a href="javascript:">向右走</</SPAN>a>
   <</SPAN>div id="div1">
       <</SPAN>ul>
           <</SPAN>li><</SPAN>img src="image/1.jpg"></</SPAN>li>
           <</SPAN>li><</SPAN>img src="image/2.jpg"></</SPAN>li>
           <</SPAN>li><</SPAN>img src="image/3.jpg"></</SPAN>li>
           <</SPAN>li><</SPAN>img src="image/4.jpg"></</SPAN>li>
       </</SPAN>ul>
   </</SPAN>div>
</</SPAN>body>
</</SPAN>html>

*{
margin:0;
padding: 0;
}
#div1{
overflow: hidden;
background: blue;
position: relative;
width: 600px;
height: 150px;
margin:100px auto;
}
#div1 ul{
position: absolute;
left: 0px;
top: 0px;
list-style: none;
}
#div1 ul li{
float: left;
}
#div1 ul li img{
width:150px;
height:150px;
}

js:代码
window.onload=function(){
var oDiv=document.getElementById(‘div1‘);
var oUl=oDiv.getElementsByTagName(‘ul‘)[0];
var aLi=oUl.getElementsByTagName(‘li‘);
var timer=null;
var speed=2;//
控制滚动速度以及方向
oUl.innerHTML=oUl.innerHTML+oUl.innerHTML;
oUl.style.width=aLi[0].offsetWidth*aLi.length+‘px‘;
timer=setInterval(move,30);

oDiv.onmouseover=function(){//
鼠标移入暂定
clearInterval(timer);
};
oDiv.onmouseout=function(){//
鼠标移出继续滚动
timer=setInterval(move,30);
}

document.getElementsByTagName(‘a‘)[0].onclick=function(){
speed=-2;
}
document.getElementsByTagName(‘a‘)[1].onclick=function(){
speed=2;
}

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‘;
}

}


 

无缝滚动

标签:

原文地址:http://www.cnblogs.com/ttsy/p/4195267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!