css js 文字滾動效果

來源:互聯網
上載者:User

html代碼
<div class="box" id="marqueebox0">
 <ul>
  <li style="background:#f8e2ac;">第一行</li>
  <li style="background:#f5f5f5;">第二行</li>
  <li style="background:#ffe6ec;">第三行</li>
 </ul>
</div>


  2、css教程代碼
.box{width:150px; height:25px;line-height:25px; border:#bbb 1px solid; overflow:hidden;}
.box ul{margin:0; padding:0}
.box li{height:25px; line-height:25px; font-size:12px; text-align:center; list-style-type:none;}


  3、js代碼
function startmarquee(lh,speed,delay,index){
/*
函數startmarquee的參數:
lh:文字一次向上滾動的距離或高度;
speed:捲動速度;
delay:滾動停頓的時間間隔;
index:可以使封裝後的函數應用於頁面當中不同的元素;
*/
    var t;
    var p=false;
    var o=document.getelementbyid("marqueebox"+index);
//擷取文檔中的捲動區域對象,賦值給變數o;
    o.innerhtml+=o.innerhtml; //對象中的實際內容被複製了一份,包含了兩個ul,當然li標籤也由原來的3行,變為6行;複製的目的在於給文字不間斷向上滾動提供過渡。
    o.onmouseo教程ver=function(){p=true}
//滑鼠滑過,停止滾動;
    o.onmouseout=function(){p=false}
//滑鼠離開,開始滾動;p是true還是false直接影響到下面start()函數的執行;
    o.scrolltop = 0;
//文字內容頂端與捲動區域頂端的距離,初始值為0;
    function start(){
        t=setinterval(scrolling,speed); //每隔一段時間,setinterval便會執行一次scrolling函數;speed越大,滾動時間間隔越大,捲動速度越慢;
        if(!p){ o.scrolltop += 1;}
//滾動停止或開始,取決於p傳來的布爾值;
    }
    function scrolling(){
        if(o.scrolltop%lh!=0){
//如果不被整除,即一次上移的高度達不到lh,則內容會繼續往上滾動;
            o.scrolltop += 1;
            if(o.scrolltop>=o.scrollheight/2) o.scrolltop = 0;
            //對象o中的內容之前被複製了一次,所以它的滾動高度,其實是原來內容的兩倍高度;當內容向上滾動到scrollheight/2的高度時,全部3行文字已經顯示了一遍,至此整塊內容scrolltop歸0;再等待下一輪的滾動,從而達到文字不間斷向上滾動的效果;
        }else{
            clearinterval(t);
//否則清除t,暫停滾動
            settimeout(start,delay);
//經過delay間隔後,啟動start() 再連續滾動
        }
    }
    settimeout(start,delay);
//第一次啟動滾動;settimeout會在一定的時間後執行函數start(),且只執行一次
}
//傳遞參數
startmarquee(25,30,3000,0);
//帶停頓效果
startmarquee(25,40,0,1);
//不間斷連續

相關文章

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.