簡體   English   中英

實現了JS打字機效果,無法弄清楚如何使它在最后一個數組項之后循環

[英]Achieved JS typewriter effect, can't figure out how to make it loop after last array item

我已經達到了我期望的打字機效果。 問題是,當到達數組的最后一項時,循環不會重新開始,這就是我希望弄清楚如何實現的效果。

這是我的筆: http : //codepen.io/marlee/pen/MeYOZd

這是代碼:

HTML:

<div class="normalText">Flex allows you to manage information relating to <span id="changeText">customers.</span></div>

CSS:

body {
background: #333;
color: #eee;
font-family: Consolas, monaco, monospace;
}

使用Javascript:

var container = document.getElementById('changeText');

var things = ['investors.','clients.', 'stakeholders.', 'students.',    'properties.'];
var t = -1;
var thing = '';
var message = container.innerHTML;
var mode = 'write';
var delay = 1000;

function updateText(txt) {
  container.innerHTML = txt;
}

function tick() {

    if(container.innerHTML.length == 0) {
        t++;
        thing = things[t];
        message = '';
        mode = 'write';
     }

switch(mode) {
    case 'write' :
        message += thing.slice(0, 1);
        thing = thing.substr(1);

        updateText(message);

        if(thing.length === 0 && t === (things.length - 1)) {
            window.clearTimeout(timeout);
            return;
        }

        if(thing.length == 0){
            mode = 'delete';
            delay = 1500;
        } else {
            delay = 32 + Math.round(Math.random() * 40);
        }

        break;

    case 'delete' :
        message = message.slice(0, -1);
        updateText(message);

        if(message.length == 0)
        {
            mode = 'write';
            delay = 1500;
        } else {
            delay = 32 + Math.round(Math.random() * 100);
        }
        break;
    }

    timeout = window.setTimeout(tick, delay);
}

var timeout = window.setTimeout(tick, delay);

我改了

您可以在這里查看差異: https : //www.diffnow.com/?report=diqrj

 var container = document.getElementById('changeText'); var things = ['investors.','clients.', 'stakeholders.', 'students.', 'properties.']; var t = -1; var thing = ''; var message = container.innerHTML; things.push(message); var mode = 'write'; var delay = 1000; function updateText(txt) { container.innerHTML = txt; } function tick() { if(container.innerHTML.length == 0) { t = (t+1) % things.length; thing = things[t]; message = ''; mode = 'write'; } switch(mode) { case 'write' : message += thing.slice(0, 1); thing = thing.substr(1); updateText(message); if(thing.length == 0){ mode = 'delete'; delay = 1500; } else { delay = 32 + Math.round(Math.random() * 40); } break; case 'delete' : message = message.slice(0, -1); updateText(message); if(message.length == 0) { mode = 'write'; delay = 1500; } else { delay = 32 + Math.round(Math.random() * 100); } break; } timeout = window.setTimeout(tick, delay); } var timeout = window.setTimeout(tick, delay); 
 body { background: #333; color: #eee; font-family: Consolas, monaco, monospace; } 
 <div class="normalText">Flex allows you to manage information relating to <span id="changeText">customers.</span></div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM