常用JS图片滚动(无缝、平滑、上下左右滚动)代码大全(推荐)
程序员文章站
2022-06-23 22:10:19
废话不多说了,直接给大家贴代码了,具体代码如下所示:
<----->
废话不多说了,直接给大家贴代码了,具体代码如下所示:
<head> <-----> </head> <body> <!--向下滚动代码开始--> <div id="colee" style="overflow:hidden;height:253px;width:410px;"> <div id="colee1"> <p><img src=""></p> <p><img src=""></p> <p><img src=""></p> <p><img src=""></p> <p><img src=""></p> <p><img src=""></p> <p><img src=""></p> <p><img src=""></p> <p><img src=""></p> </div> <div id="colee2"></div> </div> <script> var speed=30; var colee2=document.getelementbyid("colee2"); var colee1=document.getelementbyid("colee1"); var colee=document.getelementbyid("colee"); colee2.innerhtml=colee1.innerhtml; //克隆colee1为colee2 function marquee1(){ //当滚动至colee1与colee2交界时 if(colee2.offsettop-colee.scrolltop<=0){ colee.scrolltop-=colee1.offsetheight; //colee跳到最顶端 }else{ colee.scrolltop++ } } var mymar1=setinterval(marquee1,speed)//设置定时器 //鼠标移上时清除定时器达到滚动停止的目的 colee.onmouseover=function() {clearinterval(mymar1)} //鼠标移开时重设定时器 colee.onmouseout=function(){mymar1=setinterval(marquee1,speed)} </script> <!--向上滚动代码结束--> <br> <!--下面是向下滚动代码--> <div id="colee_bottom" style="overflow:hidden;height:253px;width:410px;"> <div id="colee_bottom1"> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> <p><img src="/jscss/demoimg/200907/bg3.jpg"></p> </div> <div id="colee_bottom2"></div> </div> <script> var speed=30 var colee_bottom2=document.getelementbyid("colee_bottom2"); var colee_bottom1=document.getelementbyid("colee_bottom1"); var colee_bottom=document.getelementbyid("colee_bottom"); colee_bottom2.innerhtml=colee_bottom1.innerhtml colee_bottom.scrolltop=colee_bottom.scrollheight function marquee2(){ if(colee_bottom1.offsettop-colee_bottom.scrolltop>=0) colee_bottom.scrolltop+=colee_bottom2.offsetheight else{ colee_bottom.scrolltop-- } } var mymar2=setinterval(marquee2,speed) colee_bottom.onmouseover=function() {clearinterval(mymar2)} colee_bottom.onmouseout=function() {mymar2=setinterval(marquee2,speed)} </script> <!--向下滚动代码结束--> <br> <!--下面是向左滚动代码--> <div id="colee_left" style="overflow:hidden;width:500px;"> <table cellpadding="0" cellspacing="0" border="0"> <tr><td id="colee_left1" valign="top" align="center"> <table cellpadding="2" cellspacing="0" border="0"> <tr align="center"> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> </tr> </table> </td> <td id="colee_left2" valign="top"></td> </tr> </table> </div> <script> //使用div时,请保证colee_left2与colee_left1是在同一行上. var speed=30//速度数值越大速度越慢 var colee_left2=document.getelementbyid("colee_left2"); var colee_left1=document.getelementbyid("colee_left1"); var colee_left=document.getelementbyid("colee_left"); colee_left2.innerhtml=colee_left1.innerhtml function marquee3(){ if(colee_left2.offsetwidth-colee_left.scrollleft<=0)//offsetwidth 是对象的可见宽度 colee_left.scrollleft-=colee_left1.offsetwidth//scrollwidth 是对象的实际内容的宽,不包边线宽度 else{ colee_left.scrollleft++ } } var mymar3=setinterval(marquee3,speed) colee_left.onmouseover=function() {clearinterval(mymar3)} colee_left.onmouseout=function() {mymar3=setinterval(marquee3,speed)} </script> <!--向左滚动代码结束--> <br> <!--下面是向右滚动代码--> <div id="colee_right" style="overflow:hidden;width:500px;"> <table cellpadding="0" cellspacing="0" border="0"> <tr><td id="colee_right1" valign="top" align="center"> <table cellpadding="2" cellspacing="0" border="0"> <tr align="center"> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> <td><p><img src="/jscss/demoimg/200907/bg3.jpg"></p></td> </tr> </table> </td> <td id="colee_right2" valign="top"></td> </tr> </table> </div> <script> var speed=30//速度数值越大速度越慢 var colee_right2=document.getelementbyid("colee_right2"); var colee_right1=document.getelementbyid("colee_right1"); var colee_right=document.getelementbyid("colee_right"); colee_right2.innerhtml=colee_right1.innerhtml function marquee4(){ if(colee_right.scrollleft<=0) colee_right.scrollleft+=colee_right2.offsetwidth else{ colee_right.scrollleft-- } } var mymar4=setinterval(marquee4,speed) colee_right.onmouseover=function() {clearinterval(mymar4)} colee_right.onmouseout=function() {mymar4=setinterval(marquee4,speed)} </script> <!--向右滚动代码结束--> </body> </html>
以上所述是小编给大家介绍的常用js图片滚动(无缝、平滑、上下左右滚动)代码大全(推荐),希望对大家有所帮助