2017-10-17 27 views
1

我已经调整了这个代码,我发现我几乎完美地完成了它,除了它继续循环。我希望它只是输入“零”,删除,键入“一”,然后停止。我试过在这里修改一些东西,但不能让它起作用。使javascript键入循环轮回一次

<span 
    class="txt-rotate" 
    style="font-size: 72px; font-weight: 500; line-height: 72px; margin-bottom: 20px; color: white;" 
    data-period="2000" 
    data-rotate='[ "zero.", "one." ]'></span> 

<script> 
var TxtRotate = function(el, toRotate, period) { 
    this.toRotate = toRotate; 
    this.el = el; 
    this.loopNum = 0; 
    this.period = parseInt(period, 10) || 2000; 
    this.txt = ''; 
    this.tick(); 
    this.isDeleting = false; 
}; 

TxtRotate.prototype.tick = function() { 
    var i = this.loopNum % this.toRotate.length; 
    var fullTxt = this.toRotate[i]; 

    if (this.isDeleting) { 
    this.txt = fullTxt.substring(0, this.txt.length - 1); 
    } else { 
    this.txt = fullTxt.substring(0, this.txt.length + 1); 
    } 

    this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>'; 

    var that = this; 
    var delta = 300 - Math.random() * 100; 

    if (this.isDeleting) { delta /= 2; } 

    if (!this.isDeleting && this.txt === fullTxt) { 
    delta = this.period; 
    this.isDeleting = true; 
    } else if (this.isDeleting && this.txt === '') { 
    this.isDeleting = false; 
    this.loopNum++; 
    delta = 500; 
    } 

    setTimeout(function() { 
    that.tick(); 
    }, delta); 
}; 

window.onload = function() { 
    var elements = document.getElementsByClassName('txt-rotate'); 
    for (var i=0; i<elements.length; i++) { 
    var toRotate = elements[i].getAttribute('data-rotate'); 
    var period = elements[i].getAttribute('data-period'); 
    if (toRotate) { 
     new TxtRotate(elements[i], JSON.parse(toRotate), period); 
    } 
    } 
    // INJECT CSS 
    var css = document.createElement("style"); 
    css.type = "text/css"; 
    css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #fff }"; 
    document.body.appendChild(css); 
}; 
</script> 

有关如何实现此目的的任何想法?

回答

0

使用:

TxtRotate.prototype.tick = function() { 
    //Don't use modulo, it causes the loop. If there is no full text, you are out of the loop and you don't want to do anything. 
    var fullTxt = this.toRotate[this.loopNum]; 
    if(fullTxt) { 
    if (this.isDeleting) { 
     this.txt = fullTxt.substring(0, this.txt.length - 1); 
    } else { 
     this.txt = fullTxt.substring(0, this.txt.length + 1); 
    } 

    this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>'; 

    var that = this; 
    var delta = 300 - Math.random() * 100; 

    if (this.isDeleting) { delta /= 2; } 

    if (!this.isDeleting && this.txt === fullTxt) { 
     // If you are on the last item of the toRotate array, break the loop before deletion. 
     if(this.loopNum >= this.toRotate.length-1) { 
     return; 
     } 
     delta = this.period; 
     this.isDeleting = true; 
    } else if (this.isDeleting && this.txt === '') { 
     this.isDeleting = false; 
     this.loopNum++; 
     delta = 500; 
    } 

    setTimeout(function() { 
     that.tick(); 
    }, delta); 
    } 
}; 

希望这有助于;)

+0

超级快,谢谢!但有一件事......它删除了最后的“一个”。当它完成时,我希望这个在页面上留下来......我该怎么做到这一点?再次感谢! – NikkIC

+0

我将不得不防止删除发生在最后一次数据旋转。我要检查它,我会编辑我的答案并发布另一条评论以保持联系。 – sjahan

+0

现在应该没问题了)但是,请注意,我从你的代码片开始工作,但我认为应该有更清晰的方法来达到预期的结果! – sjahan