JS實現(xiàn)簡單的鍵盤打字的效果
更新時間:2015年04月24日 10:37:21 投稿:hebedich
本文給大家分享的是使用javascript實現(xiàn)的簡單的鍵盤打字效果,十分的簡單實用,推薦給有需要的小伙伴參考下。
以代碼形式實現(xiàn)過程分析:
<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)后停留時間 var TextTime = 100; //每條信息中的字出現(xiàn)的間隔時間 var newsi = 0; var txti = 0; var txttimer; //調(diào)用setInterval的返回值,用于取消對函數(shù)的周期性執(zhí)行 var newstimer; var newstitle = new Array(); //以數(shù)組形式保存每個信息的標(biāo)題 var newshref = new Array(); //以數(shù)組形式保存信息標(biāo)題的鏈接 newstitle[0] = "歡迎來到我的博客"; //顯示在網(wǎng)頁上的文字內(nèi)容和對應(yīng)的鏈接 newshref[0] = "http://www.dbjr.com.cn/guihailiuli/"; newstitle[1] = "http://www.dbjr.com.cn/guihailiuli/"; newshref[1] = "http://www.dbjr.com.cn/guihailiuli/"; newstitle[2] = "博客園歡迎你哦"; newshref[2] = "http://www.dbjr.com.cn"; newstitle[3] = "ByeBye~~"; newshref[3] = "http://www.dbjr.com.cn"; function shownew(){ hwnewstr=newstitle[newsi]; //通過newsi傳遞,依次顯示數(shù)組中的內(nèi)容 newslink=newshref[newsi]; //依次顯示文字對應(yīng)的鏈接 if(txti>=hwnewstr.length){ clearInterval(txttimer); //一旦超過要顯示的文字長度,清除對shownew()的周期性調(diào)用 clearInterval(newstimer); newsi++; //顯示數(shù)組中的下一個 if(newsi>=newstitle.length){ newsi = 0; //當(dāng)newsi大于信息標(biāo)題的數(shù)量時,把newsi清零,重新從第一個顯示 } 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); //截取字符,從第一個字符到txti+1個字符 txti++; //文字一個個出現(xiàn) txttimer = setInterval("shownew()",TextTime); } shownew(); </script> </body> </html>
以上所述就是本文的全部內(nèi)容了,希望能夠給大家學(xué)習(xí)javascript有些幫助。
相關(guān)文章
Bootstrap modal使用及點擊外部不消失的解決方法
這篇文章主要為大家詳細(xì)介紹了Bootstrap modal使用及點擊外部不消失的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12JavaScript實現(xiàn)將UPC轉(zhuǎn)換成ISBN的方法
這篇文章主要介紹了JavaScript實現(xiàn)將UPC轉(zhuǎn)換成ISBN的方法,涉及javascript字符串操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05Array, Array Constructor, for in loop, typeof, instanceOf
雖然在 JavaScript 中數(shù)組是是對象,但是沒有好的理由去使用 `for in` 循環(huán) 遍歷數(shù)組。相反,有一些好的理由不去使用 for in 遍歷數(shù)組。2011-09-09