javascript之迴圈停頓上下滾動_javascript技巧

來源:互聯網
上載者:User
extractNodes函數解決各瀏覽器的節點計算問題。FireFox把分行符號也算成一個節點,很不合理啊。
先用appendChild複製前面的四個節點到底部,使得頭尾是一樣的內容,以作後用。記得做flash補間迴圈運動的時候就是這樣要頭尾一樣。
通過setInterval隔時運行rolltxt函數。
rolltxt函數首先計算的是間隔停頓的高度,滾動多高就要停頓一下,這個高度我也不知道怎麼算出來的,跟CSS有關係,我是試出來的  
用求餘數的方法可以判斷是否已經滾過了所設的間隔高度。如果餘數不等於0就繼續加1滾動。如果餘數為0說明已經滾動了所設的間隔高度,要在這裡停頓了。
設一個變數settime來計算時間,加到50的時候settime清零,滾動繼續,這個50我沒算過單位是什麼,反正隨我高興。
差不到這樣已經能停頓滾動了,但是還有一個問題,沒有限定最高值,滾動一直滾啊滾啊,當然是會出毛病了。滾到哪再重頭開始滾好呢?當然是滾到尾部跟頭一樣位置的時候,網頁高度減去可見高度就對了,為什麼這麼算?這個自己比劃比劃就知道了。
迴圈停頓上下滾動已經大功告成了,不信你試試 
<!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=gb2312" /> <title>迴圈停頓上下滾動</title> <style type="text/css"> #block2{height:23px;width:500px;border:#ccc 1px solid;font-size:12px;text-align:center;} #block2 ul{list-style:outside none none;height:17px;padding:3px 0;overflow:hidden;margin:0;} #block2 ul li{float:left;display:inline;margin:2px 0;height:14px;width:24.9%;} #block2 ul li a{text-decoration:none;} </style> </head> <body> <div id="block2"> <ul id="rolltxt"> <li>雲棲社區1</li> <li>雲棲社區2</li> <li>雲棲社區3</li> <li>雲棲社區4</li> <li>雲棲社區5</li> <li>雲棲社區6</li> <li>雲棲社區7</li> <li>雲棲社區8</li> <li>雲棲社區9</li> <li>雲棲社區10</li> <li>雲棲社區11</li> <li>雲棲社區12</li> <li>雲棲社區13</li> <li>雲棲社區14</li> <li>雲棲社區15</li> <li>雲棲社區16</li> </ul> <script type="text/javascript"> function extractNodes(pNode){ if(pNode.nodeType == 3)return null; var node,nodes = new Array(); for(var i=0;node= pNode.childNodes[i];i++){ if(node.nodeType == 1)nodes.push(node); } return nodes; } var obj=document.getElementById("rolltxt"); for(i=0;i<4;i++){ obj.appendChild(extractNodes(obj)[i].cloneNode(true)); } settime=0; var t=setInterval(rolltxt,50); function rolltxt(){ if(obj.scrollTop % (obj.clientHeight-5) ==0){ settime+=1; if(settime==50){ obj.scrollTop+=1; settime=0; } }else{ obj.scrollTop+=1; if(obj.scrollTop==(obj.scrollHeight-obj.clientHeight)){ obj.scrollTop=0; } } } obj.onmouseover=function(){clearInterval(t)} obj.onmouseout=function(){t=setInterval(rolltxt,50)} </script> </div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.