HOME  >>>  HTML整理ノート  >>> DHTML --  基本編  応用編

1文字ずつテキスト表示

 

 

Sample Source......................................................................>>>

<script language="JavaScript"><!--
var txt1 = "Dynamic HTML へようこそ!!";
var txt2 = "(Kodayan)";
var t1,t2;    //取り出した文字列
var n = 1;
var timerID,timerH;
function ViewTxt(){     //1文字ずつ表示
  if(n<=(txt1.length+txt2.length)){
    if(n <= txt1.length){
      t1 = txt1.substring(0,n);
      dv1.innerHTML = t1;
    }else{
      t2 = txt2.substring(0,(n-txt1.length));
      dv1.innerHTML = t1 + "<br>" + t2;
    }
    n++;
    timerID = setTimeout("ViewTxt()",400);
  }else{clearTimeout(timerID);}
}
function HideTxt(){    //1文字ずつ消していく
  if(n >= 0){
    if(n>txt1.length){
      t2 = txt2.substring(0,(n-txt1.length));
      dv1.innerHTML = t1 + "<br>" + t2;
    }else{
      t1 = txt1.substring(0,n);
      dv1.innerHTML = t1;
    }
    n--;
    timerH = setTimeout("HideTxt()",400);
  }else{clearTimeout(timerH);}
}
//--></script>
</head>

<body onload="ViewTxt();">
<div id="dv1" style="height:70;color:navy;
    font:20pt 'Times New Roman';text-align:center">
</div>

<button onclick="HideTxt();">テキストを消す</button>
<button onclick="ViewTxt();">テキストを表示</button>


end(最終更新:12/11/16)