做用:向左滾動的輪播圖
參數:主容器的id
調用方式:直接拷貝調用方法
function autoPlay(id){
var scroll=document.getElementById(id);
var ul=scroll.children[0]
var num=0
var timer=null;
timer=setInterval(autoGoing,20); //滾動軸方法
function autoGoing(){
num--;
num<=-1200 ? num=0:num;
ul.style.left=num+"px";
}
scroll.onmouseover=function(){ //鼠標懸停中止滾動
clearInterval(timer)
}
scroll.onmouseout=function(){ //鼠標移開 繼續滾動
timer=setInterval(autoGoing,20);
}
}
參考標籤:
<body>
<div class="box" id="scroll">
<ul>
<li><img src="img/01.jpg"/></li>
<li><img src="img/02.jpg"/></li>
<li><img src="img/03.jpg"/></li>
<li><img src="img/04.jpg"/></li>
<li><img src="img/01.jpg"/></li>
<li><img src="img/02.jpg"/></li>
</ul>
</div>
參考樣式:
*{margin: 0;padding: 0;}
ul{list-style: none;}
img{
vertical-align: top; /*取消三個圖片底部像素距離*/
}
.box{
width: 600px;
height: 200px;
margin: 100px auto;
position: relative;
overflow: hidden;
}
.box ul li {
float: left;
}
.box ul{
width: 400%;
position: absolute;
left: 0;
top: 0;
}
------------------------------------------------------------------------------------------------------------------------
|