js代码实现无缝滚动(文字和图片)
一款适用于方案和图片的JS无缝滚动代码,可控制向左或向右滚动,代码不算复杂,而且本无缝滚动代码兼容性也挺好,你几乎不需要修改什么代码,就能使用了。测试时候把那些带标号的方块都改成图片吧,这样就更有感觉了。
代码如下:
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>无缝滚动</title>
<style>
*{margin:0;padding:0;}
.other{width:100%;height:50px;background:#F00;}
.main{width:400px;height:130px;margin:0auto;border:1pxsolid#000;overflow:hidden;position:relative;}
.bannera{display:block;float:left;margin:15px0015px;}
.banner{position:absolute;width:800%;}
#wrap1,
#wrap2{float:left;}
.text{text-align:center;}
#wrap1a,
#wrap2a{text-decoration:none;}
#wrap1span,
#wrap2span{display:block;width:100px;height:100px;border:1pxsolid#000;font-size:36px;text-align:center;background:#999;}
</style>
</head>
<body>
<divclass="other">
</div>
<divclass="text">
<ahref="javascript:void(0)"id="left">向左</a><ahref="javascript:void(0)"id="right">向右</a>
</div>
<divclass="main"id="main">
<divclass="banner"id="banner">
<divid="wrap1">
<ahref="#"><span>1</span></a><ahref="#"><span>2</span></a><ahref="#"><span>3</span></a><ahref="#"><span>4</span></a><ahref="#"><span>5</span></a>
</div>
<divid="wrap2">
</div>
</div>
</div>
<divclass="other">
</div>
</body>
</html>
<scripttype="text/javascript">
window.onload=function(){
speed=-1;
move=getId('banner');
getId('wrap2').innerHTML=getId('wrap1').innerHTML;
vartime=setInterval(automove,10);
getId('main').onmouseover=function(){
clearInterval(time);
}
getId('main').onmouseout=function(){
time=setInterval(automove,10);
}
getId('left').onclick=function(){
speed=-1;
}
getId('right').onclick=function(){
speed=1;
}
}
functiongetId(id){
returndocument.getElementById(id);
}
functionautomove(){
if(parseInt(move.style.left)<=-getId('wrap1').offsetWidth){
move.style.left=0+'px';
}
if(move.offsetLeft>0){
move.style.left=-getId('wrap1').offsetWidth+'px';
}
move.style.left=move.offsetLeft+speed+'px';
}
</script>
以上所述就是本文针对js代码实现无缝滚动(文字和图片),希望大家喜欢。