-
一款适用于方案和图片的JS无缝滚动代码,可控制向左或向右滚动,代码不算复杂,而且本无缝滚动代码兼容性也挺好,你几乎不需要修改什么代码,就能使用了。测试时候把那些带标号的方块都改成图片吧,这样就更有感觉了。
-
<head>
-
<meta http-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: 0 auto; border: 1px solid #000; overflow: hidden; position: relative; }
-
.banner a { display: block; float: left; margin: 15px 0 0 15px; }
-
.banner { position: absolute; width: 800%; }
-
#wrap1,
-
#wrap2 { float: left; }
-
.text { text-align: center; }
-
#wrap1 a,
-
#wrap2 a { text-decoration: none; }
-
#wrap1 span,
-
#wrap2 span { display: block; width: 100px; height: 100px; border: 1px solid #000; font-size: 36px; text-align: center; background: #999; }
-
</style>
-
</head>
-
<body>
-
<div class=\"other\">
-
</div>
-
<div class=\"text\">
-
<a href=\"javascript:void(0)\" id=\"left\">向左</a> <a href=\"javascript:void(0)\" id=\"right\">向右</a>
-
</div>
-
<div class=\"main\" id=\"main\">
-
<div class=\"banner\" id=\"banner\">
-
<div id=\"wrap1\">
-
<a href=\"#\"><span>1</span></a><a href=\"#\"><span>2</span></a><a href=\"#\"><span>3</span></a><a href=\"#\"><span>4</span></a><a href=\"#\"><span>5</span></a>
-
</div>
-
<div id=\"wrap2\">
-
</div>
-
</div>
-
</div>
-
<div class=\"other\">
-
</div>
-
</body>
-
</html>
-
<script type=\"text/javascript\">
-
window.onload=function(){
-
speed=-1;
-
move=getId(\'banner\');
-
getId(\'wrap2\').innerHTML=getId(\'wrap1\').innerHTML;
-
var time=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;
-
}
-
}
-
function getId(id){
-
return document.getElementById(id);
-
}
-
function automove(){
-
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>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29737856/viewspace-1239963/
,如需转载,请注明出处,否则将追究法律责任。