JS左右無縫滾動(一般方法+物件導向方法)

來源:互聯網
上載者:User

複製代碼 代碼如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>JS左右無縫滾動(一般方法+物件導向方法)</title>
<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>
<!--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的解析不一樣,所以ie下必須減大於等於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-->
<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>

</head>

<body>
<div id="wrap">
<ul class="clearfix">
<li><a href="#"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
<li><a href="#"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
<li><a href="#"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
<li><a href="#"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
<li><a href="#"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" /></a></li>
</ul>
</div>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.