1. 程式人生 > >JS打字特效 JS 原生程式碼

JS打字特效 JS 原生程式碼

<html>
<head>
<title>打字效果的帶連結的新聞標題</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
body{font-size:14px;font-weight:bold;}
</style>
</head>
<body>
最新內容:<a id="HotNews" href="" target="_blank"></a>
<script>
var NewsTime = 2000;	//每條新聞的停留時間
var TextTime = 50;		//新聞標題文字出現等待時間,越小越快
var newsi = 0;
var txti = 0;
var txttimer;
var newstimer;
var newstitle = new Array();	//新聞標題
var newshref = new Array();		//新聞連結
newstitle[0] = "Jensen_Yao的部落格";
newshref[0] = "http://blog.csdn.net/jensen_yao";
newstitle[1] = "http://blog.csdn.net/jensen_yao";
newshref[1] = "http://blog.csdn.net/jensen_yao";
newstitle[2] = "最高的效率就是對現有材料的最佳利用。";
newshref[2] = "http://blog.csdn.net/jensen_yao";
newstitle[3] = "學會偷懶!";
newshref[3] = "http://blog.csdn.net/jensen_yao";
newstitle[4] = "hello!";
newshref[4] = "http://blog.csdn.net/jensen_yao";
function shownew()
{
  var endstr = "_";
  hwnewstr = newstitle[newsi];
  newslink = newshref[newsi];
  if(txti==(hwnewstr.length-1)){endstr="";}
  if(txti>=hwnewstr.length){
    clearInterval(txttimer);
    clearInterval(newstimer);
    newsi++;
    if(newsi>=newstitle.length){
      newsi = 0
    }
    newstimer = setInterval("shownew()",NewsTime);
    txti = 0;
    return;
   }
   clearInterval(txttimer);
   document.getElementById("HotNews").href=newslink;
   document.getElementById("HotNews").innerHTML = hwnewstr.substring(0,txti+1)+endstr;
   txti++;
   txttimer = setInterval("shownew()",TextTime);
 }
shownew();
</script>
<p><a href="http://www.108js.com" target="_blank">Jensen_Yao的部落格</a></p>
</body>
</html>