無縫滾動的核心技巧只有一句:把內容複製一份接在自己後面,然後在位移滾過一半寬度時瞬間歸零。因為前後兩段一模一樣,歸零的那一刻視覺上完全看不出接縫。
<script type="text/javascript">
window.onload=function(){
var wrap=document.getElementById("wrap");
var wrap_ul=wrap.getElementsByTagName("ul")[0];
var wrap_li=wrap.getElementsByTagName("li");
wrap_ul.innerHTML+=wrap_ul.innerHTML;
wrap_ul.style.width=(wrap_li[0].offsetWidth+20)*wrap_li.length+"px";
Autoscroll=setInterval(scroll,100);
function scroll(){
wrap_ul.style.left=wrap_ul.offsetLeft-3+"px";// IE 對 offsetLeft 的解析略有差異,所以每次至少減 3
if(wrap_ul.offsetLeft<=-wrap_ul.offsetWidth/2){
wrap_ul.style.left="0px";
}else if(wrap_ul.offsetLeft>=0){
wrap_ul.style.left=-wrap_ul.offsetWidth/2+"px";
}
}
wrap.onmouseover=function(){
clearInterval(Autoscroll);
}
wrap.onmouseout=function(){
Autoscroll=setInterval(scroll,100);
}
}
</script>
幾個要點:innerHTML 自我附加完成複製,寬度要記得把 li 的 margin 一起算進去(這裡是左右各 10),offsetWidth/2 就是歸零的臨界點。往左滾時 offsetLeft 會越來越小,滾到 -一半寬 就拉回 0;反方向滾時 offsetLeft 變正,滾到 0 以上就跳回 -一半寬。
把容器、方向、速度都參數化,用 new 就能開一個新的滾動實例:
<script type="text/javascript">
function Slide(obj,direction,speed){ // speed 需 >= 3,IE 下才不會出現間隙
this.container=document.getElementById(obj);
this.content=this.container.getElementsByTagName("ul")[0];
this.lis=this.content.getElementsByTagName("li");
this.content.innerHTML+=this.content.innerHTML;
this.content.style.width=(this.lis[0].offsetWidth+20)*this.lis.length+"px";
var that=this;
if(direction=="left"){
this.speed=-speed;
}else if(direction=="right"){
this.speed=speed;
}
Slide.prototype.scroll=function(){
this.content.style.left=this.content.offsetLeft+this.speed+"px";
if(this.content.offsetLeft <= -this.content.offsetWidth/2){
this.content.style.left="0px";
}else if(this.content.offsetLeft >= 0){
this.content.style.left = -this.content.offsetWidth/2 + "px";
}
}
this.time=setInterval(function(){that.scroll()},100);
this.container.onmouseover=function(){
clearInterval(that.time);
}
this.container.onmouseout=function(){
that.time=setInterval(function(){that.scroll()},100);
}
}
</script>
<script type="text/javascript">
window.onload=function(){new Slide("wrap","left",5)}
</script>
<style type="text/css">
.clearfix:after {content:"."; display:block; height:0; visibility:hidden; clear:both; }
.clearfix { *zoom:1; }
body{background:gray;}
#wrap{width:810px; height:160px;
border:1px solid black;
position:relative;
left:50%;
top:50%;
margin-left:-410px;
margin-top:200px;
background:#fff;
overflow:hidden;}
#wrap ul{margin:0px;
padding:0px;
position:absolute;
top:0px;
left:0px;}
#wrap ul li{ list-style:none; float:left;margin:5px 10px;width:265px;}
#wrap ul li img{ border:1px black solid; padding:10px;}
</style>
<div id="wrap">
<ul class="clearfix">
<li><a href="#"><img src="https://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
<li><a href="#"><img src="https://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
<li><a href="#"><img src="https://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
<li><a href="#"><img src="https://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
<li><a href="#"><img src="https://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
</ul>
</div>
第一,物件導向版本把 scroll 函式掛在 prototype 上,這在 JS 裡其實有陷阱:prototype 上的方法被所有實例共享,若同頁面開啟兩個 Slide,後建立的實例會覆蓋前一個的 scroll 實作。改成在建構函式內定義 this.scroll = function(){...},或改用 ES6 class 並把狀態完整封在實例上,才是安全做法。
第二,用 requestAnimationFrame 取代 setInterval(…, 100) 會順很多——100ms 一跳本來就有輕微頓感,rAF 則會隨螢幕刷新率走,位移也能改成像素累加而非固定 3px,速度就不會再受 IE 相容性影響。