2
我已經在下面的這個腳本允許我點擊按鈕時播放帶有類型編寫器效果的文本。但是,如何改變允許我暫停的代碼,或者在再次單擊按鈕時停止腳本。Javascript打字機效果暫停/停止按鈕
HTML
<div ID="textDestination" class="news"></div>
<div class="play" id="playbtn"></div>
的JavaScript
var text="I remember the big tree in Mums yard I used to climb when they were drinking. I remember sitting up there crying cause I knew I’d have to get down, and how the look on his face made me want to laugh, how small he looked from up there. I remember he would threaten to throw things at me if I didn't get down. He knew he had no power over me up there, but he also knew eventually I would have to get down. Then he would say I was in for it whether I came down or not. But up there I was untouchable, and the world outside our house held endless possibility."
var delay=50;
var currentChar=1;
var destination="[none]";
function type()
{
//if (document.all)
{
var dest=document.getElementById(destination);
if (dest)// && dest.innerHTML)
{
dest.innerHTML=text.substr(0, currentChar)+"_";
currentChar++;
if (currentChar>text.length)
{
currentChar=1;
setTimeout("type()", 5000);
}
else
{
setTimeout("type()", delay);
}
}
}
}
function startTyping(textParam, delayParam, destinationParam)
{
text=textParam;
delay=delayParam;
currentChar=1;
destination=destinationParam;
type();
}
$('#playbtn').click(function() {
javascript:startTyping(text, 50, "textDestination");
});
馬特謝謝你的建議和代碼。但你的代碼不會循環。完成後是否可以循環文本? – DANLEE 2013-03-20 00:55:25
Upvoted :)感謝您幫助我的downvote – 2013-03-20 00:58:20
@ user1533321我無法運行您的原始代碼,因此請向我描述「循環文本」是什麼意思? – 2013-03-20 01:00:42