无缝内容滚动JS,比较简洁的无缝滚动JS,鼠标移动上去会自动停止,移除后又自动滚动
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312" />
<title>无缝内容滚动</title>
<style>
*{
margin: 0;
padding:0;
}
body{
width: 1000px;
margin: 100px auto;
background-color: #fff;
}
#wrapper{
overflow: hidden;
width: 600px;
height: 100px;
position: relative;/*必需*/
}
#wrapper ul {
position: absolute;/*必需*/
left: 0;
top: 0;
}
#wrapper li{
float: left;
list-style: none;
}
#wrapper li img{
width: 150px;
height: 100px;
border-radius: 9px;
}
input[type=button]{
margin-top: 20px;
width: 35px;
height: 25px;
line-height: 25px;
}
</style>
<script type="text/javascript">
window.onload=function(){
var timer=null;
var speed=1;//每次滚动的像素距离
var od=document.getElementById("wrapper");
var au=od.getElementsByTagName('ul')[0];
var ali=au.getElementsByTagName('li');
au.innerHTML=au.innerHTML+au.innerHTML;
au.style.width=ali[0].offsetWidth*ali.length+'px';
timer=setInterval(move,30)//滚动速度
function move(){
if(au.offsetLeft<-au.offsetWidth/2){
au.style.left='0';
}
if(au.offsetLeft>0){
au.style.left=-au.offsetWidth/2+'px';
}
au.style.left=au.offsetLeft+speed+'px';
}
od.onmouseover=function(){
clearInterval(timer);
}
od.onmouseout=function(){
timer=setInterval(move,30)//滚动速度
}
document.getElementById("btn1").onclick=function(){
speed=-1;//点击按钮后端滚动像素距离
}
document.getElementById("btn2").onclick=function(){
speed=1;//点击按钮后端滚动像素距离
}
}
</script>
</head>
<body>
<div id="wrapper">
<ul>
<li><img src="img/pic4.jpg"/></li>
<li><img src="img/pic3.jpg"/></li>
<li><img src="img/pic2.jpg"/></li>
<li><img src="img/pic1.jpg"/></li>
</ul>
</div>
<input type="button" name="" id="btn1" value="向左" />
<input type="button" id="btn2" value="向右"/>
</body>
</html>
隐藏内容需要登录才可以看见
登录





