以代碼形式實(shí)現(xiàn)過(guò)程分析:
<html><head><title>打字效果</title><meta http-equiv="Content-Type" Content="text/html;charset=gb2312" /><style type="text/css">body{ font-size:14px; font-color:#purple; font-weight:bolder;}</style></head><body>最新內(nèi)容: <a id = "Hotnews" href="" target="_blank"> </a> <script language="javascript"> var NewsTime = 2000; //每條信息完整出現(xiàn)后停留時(shí)間 var TextTime = 100; //每條信息中的字出現(xiàn)的間隔時(shí)間 var newsi = 0; var txti = 0; var txttimer; //調(diào)用setInterval的返回值,用于取消對(duì)函數(shù)的周期性執(zhí)行 var newstimer; var newstitle = new Array(); //以數(shù)組形式保存每個(gè)信息的標(biāo)題 var newshref = new Array(); //以數(shù)組形式保存信息標(biāo)題的鏈接 newstitle[0] = "歡迎來(lái)到我的博客"; //顯示在網(wǎng)頁(yè)上的文字內(nèi)容和對(duì)應(yīng)的鏈接 newshref[0] = "http://m.survivalescaperooms.com/guihailiuli/"; newstitle[1] = "http://m.survivalescaperooms.com/guihailiuli/"; newshref[1] = "http://m.survivalescaperooms.com/guihailiuli/"; newstitle[2] = "博客園歡迎你哦"; newshref[2] = "http://m.survivalescaperooms.com"; newstitle[3] = "ByeBye~~"; newshref[3] = "http://m.survivalescaperooms.com"; function shownew(){ hwnewstr=newstitle[newsi]; //通過(guò)newsi傳遞,依次顯示數(shù)組中的內(nèi)容 newslink=newshref[newsi]; //依次顯示文字對(duì)應(yīng)的鏈接 if(txti>=hwnewstr.length){ clearInterval(txttimer); //一旦超過(guò)要顯示的文字長(zhǎng)度,清除對(duì)shownew()的周期性調(diào)用 clearInterval(newstimer); newsi++; //顯示數(shù)組中的下一個(gè) if(newsi>=newstitle.length){ newsi = 0; //當(dāng)newsi大于信息標(biāo)題的數(shù)量時(shí),把newsi清零,重新從第一個(gè)顯示 } newstimer = setInterval("shownew()",NewsTime); //間隔2000ms后重新調(diào)用shownew() txti = 0; return; } clearInterval(txttimer); document.getElementById("Hotnews").href = newslink; document.getElementById("Hotnews").innerHTML = hwnewstr.substring(0,txti+1); //截取字符,從第一個(gè)字符到txti+1個(gè)字符 txti++; //文字一個(gè)個(gè)出現(xiàn) txttimer = setInterval("shownew()",TextTime); } shownew();</script></body></html>以上所述就是本文的全部?jī)?nèi)容了,希望能夠給大家學(xué)習(xí)javascript有些幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注