JavaScript中实现无缝滚动、分享到侧边栏实例代码
废话不多说,直接给大家贴代码了,代码解决一起问题!
下面一段代码给大家介绍js无缝滚动实例代码:
代码如下所示:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>无标题文档</title> <style> *{margin:0px; padding:0px;} #div1{width:830px;height:166px;margin:50pxauto; position:relative; background:white; overflow:hidden;} #div1ulli{float:left;width:174px;height:166px;list-style:none; } ul{position:absolute;} </style> <script> window.onload=function(){ varoDiv=document.getElementById('div1'); varoUl=oDiv.getElementsByTagName('ul')[0]; varaLi=oUl.getElementsByTagName('li'); varspeed=3 oUl.innerHTML=oUl.innerHTML+oUl.innerHTML; oUl.style.width=aLi[0].offsetWidth*aLi.length+'px'; functionmove(){ if(oUl.offsetLeft<-oUl.offsetWidth/2) { oUl.style.left='0'; } if(oUl.offsetLeft>0) { oUl.style.left=-oUl.offsetWidth/2+'px'; } oUl.style.left=oUl.offsetLeft+speed+'px'; }; vartimer=setInterval(move,30); oDiv.onmouseover=function(){ clearInterval(timer); }; oDiv.onmouseout=function(){ timer=setInterval(move,30); } document.getElementsByTagName('a')[0].onclick=function(){ speed=-3; }; document.getElementsByTagName('a')[1].onclick=function(){ speed=3; }; }; </script> </head> <body> <ahref="javascipt:;">向左走</a> <ahref="javascipt:;">向右走</a> <divid="div1"> <ul> <li><imgsrc="images/b01.jpg"/></li> <li><imgsrc="images/b02.jpg"/></li> <li><imgsrc="images/b03.jpg"/></li> <li><imgsrc="images/b04.jpg"/></li> <li><imgsrc="images/b05.jpg"/></li> </ul> </div> </body> </html>
分享到侧边栏js代码如下所示:
代码如下所示:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>无标题文档</title> <style> #div1{width:100px;height:150px;background:#0F0;position:absolute;left:-100px;} #div1span{position:absolute;width:20px;height:60px;line-height:20px;background:#00F;right:-20px;top:50px;} </style> <script> window.onload=function(){ varoDiv=document.getElementById('div1'); oDiv.onmouseover=function(){ startMove(10,0); } oDiv.onmouseout=function(){ startMove(-10,-100); } } vartimer=null; functionstartMove(speed,locall){ varoDiv=document.getElementById('div1'); clearInterval(timer); timer=setInterval(function(){ if(oDiv.offsetLeft==locall){ clearInterval(timer); } else{ oDiv.style.left=oDiv.offsetLeft+speed+'px'; } },30); } </script> </head> <body> <divid="div1"> <span>分享到</span> </div> </body> </html>
以上所述是小编给大家介绍的JavaScript中实现无缝滚动、分享到侧边栏实例代码,代码简单易懂,有疑问欢迎给我留言,小编及时给您答复!