不间断滚动代码

一个设定宽度并且隐藏超出它宽度的内容的容器demo,里面放demo1和demo2,demo1是滚动内容,demo2为demo1的直接克隆副本,通过不断改变demo1的scrollTop或者scrollLeft达到滚动的目的,当滚动至demo1与demo2的交界处时直接跳回初始位置,因为demo1与demo2一样,所以分不出跳动的瞬间,从而达到"无缝"滚动的目的。

向上滚动:
<div id=demo style=overflow:hidden;height:100;width:90;
background:#214984;color:#ffffff>
<div id=demo1>
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
</div>
<div id=demo2></div>
</div>
<script>
var speed=30
demo2.innerHTML=demo1.innerHTML //克隆demo1为demo2
function Marquee(){
//当滚动至demo1与demo2交界时
if(demo2.offsetTop-demo.scrollTop<=0)   
demo.scrollTop-=demo1.offsetHeight //demo跳到最顶端
else{
demo.scrollTop++
}
}
var MyMar=setInterval(Marquee,speed)//设置定时器
//鼠标移上时清除定时器达到滚动停止的目的
demo.οnmοuseοver=function() {clearInterval(MyMar)}
//鼠标移开时重设定时器
demo.οnmοuseοut=function(){MyMar=setInterval(Marquee,speed)}
&lt;/script>

向下滚动:
<div id=demo style=overflow:hidden;height:100;width:90;
background:#214984;color:#ffffff>
<div id=demo1>
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
<img src="logo.gif">
</div>
<div id=demo2></div>
</div>
<script>
var speed=30
demo2.innerHTML=demo1.innerHTML
demo.scrollTop=demo.scrollHeight
function Marquee(){
if(demo1.offsetTop-demo.scrollTop&gt;=0)
demo.scrollTop+=demo2.offsetHeight
else{
demo.scrollTop--
}
}
var MyMar=setInterval(Marquee,speed)
demo.οnmοuseοver=function() {clearInterval(MyMar)}
demo.οnmοuseοut=function() {MyMar=setInterval(Marquee,speed)}
</script>

向左滚动:
<div id="demo" style="overflow:hidden;width:670px;color:#ffffff;">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td id="demo1" valign="top" align="center">
<table cellpadding="2" cellspacing="0" border="0">
<tr align="center">
         <td><img src="logo.gif" width="88"></td>
<td><img src="logo.gif" width="88"></td>
         <td><img src="logo.gif" width="88"></td>
         <td><img src="logo.gif" width="88"></td>
<td><img src="logo.gif" width="88"></td>
</tr>
</table>
</td>
<td id="demo2" valign="top"></td>
</tr>
</table>
</div>
<script>
var speed=1//速度数值越大速度越慢
demo2.innerHTML=demo1.innerHTML
function Marquee(){
if(demo2.offsetWidth-demo.scrollLeft<=0)
demo.scrollLeft-=demo1.offsetWidth
else{
demo.scrollLeft++
}
}
var MyMar=setInterval(Marquee,speed)
demo.οnmοuseοver=function() {clearInterval(MyMar)}
demo.οnmοuseοut=function() {MyMar=setInterval(Marquee,speed)}
&lt;/script>

向右滚动:
<div id="demo" style="overflow:hidden;width:670px;color:#ffffff;">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td id="demo1" valign="top" align="center">
<table cellpadding="2" cellspacing="0" border="0">
<tr align="center">
         <td><img src="logo.gif" width="88"></td>
<td><img src="logo.gif" width="88"></td>
         <td><img src="logo.gif" width="88"></td>
         <td><img src="logo.gif" width="88"></td>
<td><img src="logo.gif" width="88"></td>
</tr>
</table>
</td>
<td id="demo2" valign="top"></td>
</tr>
</table>
</div>
<script>
var speed=1//速度数值越大速度越慢
demo2.innerHTML=demo1.innerHTML
function Marquee(){
if(demo.scrollLeft<=0)
demo.scrollLeft+=demo2.offsetWidth
else{
demo.scrollLeft--
}
}
var MyMar=setInterval(Marquee,speed)
demo.οnmοuseοver=function() {clearInterval(MyMar)}
demo.οnmοuseοut=function() {MyMar=setInterval(Marquee,speed)}
&lt;/script>

 

还有一种常用的div方法的
<div id="demo" style="overflow:hidden;height:100px;">
<div id="demo1" style="line-height:150%">
<img src='logo/(1).jpg' width=100 height=42 border=0 />
<img src='logo/(2).jpg' width=100 height=53 border=0 />
<img src='logo/(3).jpg' width=100 height=73 border=0 />
<img src='logo/(7).jpg' width=100 height=44 border=0 />
<img src='logo/(4).JPG' width=100 height=98 border=0 />
<img src='logo/(5).jpg' width=100 height=45 border=0 />
<img src='logo/(6).jpg' width=100 height=101 border=0 />
<img src='logo/(8).jpg' width=100 height=95 border=0 />
<img src='logo/(9).jpg' width=100 height=42 border=0 />
<img src='logo/(10).jpg' width=100 height=127 border=0 />
<img src='logo/(11).jpg' width=100 height=25 border=0 />
<img src='logo/(12).jpg' width=100 height=27 border=0 />
<img src='logo/(13).jpg' width=100 height=99 border=0 />
<img src='logo/(9).jpg' width=100 height=42 border=0 />
<img src='logo/(14).jpg' width=100 height=84 border=0 />
<img src='logo/(15).jpg' width=100 height=97 border=0 />
<img src='logo/(16).jpg' width=100 height=102 border=0 /></div>

<div id="demo2"></div>
</div>
<script>
var speed=40;
var demo2=document.getElementById("demo2");
var demo1=document.getElementById("demo1");
var demo=document.getElementById("demo");
demo2.innerHTML=demo1.innerHTML
function Marquee(){
if(demo2.offsetTop-demo.scrollTop<=0)
demo.scrollTop-=demo1.offsetHeight;
else{
demo.scrollTop++;
}
}
var MyMar=setInterval(Marquee,speed);
demo.οnmοuseοver=function() {clearInterval(MyMar)}
demo.οnmοuseοut=function() {MyMar=setInterval(Marquee,speed)}
&lt;/script> 

<table style="BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid" cellSpacing=0 cellPadding=0 width=750 align=center border=0> <tr> <td width="30"> <a href="#" onClick="clickdiv()" id="aa">向左 </a> </td> <td> <DIV id=demo style="OVERFLOW: hidden; WIDTH: 750px; COLOR: #ffffff"> <table cellSpacing=0 cellPadding=0 align=left border=0 cellspace="0"> <tr> <td id=demo1 vAlign=top> <table width="1710" height="116" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="171" background="pic_bg.jpg"> <div align="center">内容一 </div> </td> <td width="171" background="pic_bg.jpg"> <div align="center">内容二 </div> </td> <td width="171" background="pic_bg.jpg"> <div align="center">内容三 </div> </td> <td width="171" background="pic_bg.jpg"> <div align="center">内容四 </div> </td> <td width="171" background="pic_bg.jpg"> <div align="center">内容五 </div> </td> <td width="171" background="pic_bg.jpg"> <div align="center">内容六 </div> </td> </tr> </table> </td> <td id=demo2 vAlign=top> </td> </tr> </table> </DIV> </td> <td width="30"> <a href="#" onClick="Rclickdiv()" id="mcc">向右 </a> </td> </tr> </table> <SCRIPT> var speed3=25//速度数值越大速度越慢 var t=false; var id="aa"; document.getElementById("demo2").innerHTML=document.getElementById("demo1").innerHTML function Marquee(){ if(document.getElementById("demo2").offsetWidth-document.getElementById("demo").scrollLeft <=0) document.getElementById("demo").scrollLeft-=document.getElementById("demo1").offsetWidth else{ document.getElementById("demo").scrollLeft++ } t=false; } function RMarquee(){ if(document.getElementById("demo").scrollLeft <=0) document.getElementById("demo").scrollLeft+=document.getElementById("demo2").offsetWidth else{ document.getElementById("demo").scrollLeft-- } t=true; } function clickdiv(){ clearInterval(MyMar) Marquee(); } function Rclickdiv(){ clearInterval(MyMar) RMarquee(); } var MyMar=setInterval(Marquee,speed3) document.getElementById("demo").onmouseover=function() {clearInterval(MyMar)} document.getElementById("demo").onmouseout=function() {if(t){MyMar=setInterval(RMarquee,speed3)}else{MyMar=setInterval(Marquee,speed3)}} document.getElementById('mcc').onmouseout=function(){clearInterval(MyMar);MyMar=setInterval(RMarquee,speed3);} document.getElementById('aa').onmouseout=function(){clearInterval(MyMar);MyMar=setInterval(Marquee,speed3);} </SCRIPT> 一个简单的向左或向右移动的案例: <base href=" <a href="http://www.it365cn.com" target="_blank">http://www.it365cn.com </a>"> <div id=demo style=overflow:hidden;height:120;width:500;background:#214984;color:#ffffff> <table align=left cellpadding=0 cellspace=0 border=0> <tr> <td id=demo1 valign=top> <a href='images/logo_1.gif'> <img src="images/logo_1.gif"> </a> <!--后面的链接自己加上--> <img src="images/logo/flashempire.gif"> <img src="images/logo.gif"> <img src="images/logo/5dmedia.gif"> <img src="images/logo/macromedia.gif"> <img src="images/logo/sucaiw.gif"> <img src="images/logo/blueieda.gif"> <img src="images/logo/htmlcn.gif"> <img src="images/logo/fwcn.gif"> </td> <td id=demo2 valign=top> </td> </tr> </table> </div> <script> //向左滚动的效果 var speed=30 demo2.innerHTML=demo1.innerHTML function Marquee(){ if(demo2.offsetWidth-demo.scrollLeft <=0) demo.scrollLeft-=demo1.offsetWidth else{ demo.scrollLeft++ } } var MyMar=setInterval(Marquee,speed) demo.onmouseover=function() {clearInterval(MyMar)} demo.onmouseout=function() {MyMar=setInterval(Marquee,speed)} </script> <script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值