在无限循环中使用 jQuery 动画字宽

Animate words width with jQuery in infinite loop

我有一个 jQuery 问题,这让我的生活很艰难。

如果这个 JSBin

那么到目前为止我都做了什么

就你所见,我已经很接近了。我需要实现的是在句子中显示单词,一旦显示一个单词就显示下一个单词。直到一个句子没有宽度为 0 的单词。一旦发生这种情况,该句子就会隐藏,然后下一个句子开始。一旦最后一句话中的所有单词都可见,一切都会重新开始(无限循环)。但也必须有单词之间和句子之间延迟的功能。我现在在代码方面不知何故没有取得进展,所以我想问问是否有人有类似的问题,或者是否有一个插件可以挽救我的生命。

它应该如何工作如下;

  1. 文字必须生动(就像现在一样)
  2. 出现一个词,就叫下一个
  3. 一旦一个句子中的所有单词都出现,该句子就会隐藏。然后下一个单词开始出现在第二句中。
  4. 有些词必须有延迟,这意味着它们应该在给定的延迟后开始出现
  5. 另外我应该可以设置句子之间的间隔。
  6. 第二句说完,就该躲起来了。一切都应该重新开始。

非常感谢您的帮助。

下面的代码应该可以解决您的问题。我添加了更多的属性(数据属性),所以它更加动态。

var sentences = $('.sentence');
var maxSentenceIndex = sentences.length - 1;
var currentSentenceIndex = 0;
var currentSentence = null;

var wordClass = '.word-row';
var standardDelay = 0;
var animationDuration = 1500;
var currentWordIndex = 0;

function loadSentence() {

  currentWordIndex = 0;
  currentSentence = sentences.eq(currentSentenceIndex);

  if (currentSentenceIndex > maxSentenceIndex) {

    currentSentenceIndex = 0;



  } else {
    currentSentenceIndex++;
  }
  currentSentence.show();
  loadWord();

}

function loadWord() {

  var $sentenceWords = $(currentSentence).find(wordClass);

  var maxWordsIndex = $sentenceWords.length - 1;


  animateWords($sentenceWords.eq(currentWordIndex));

  if (currentWordIndex > maxWordsIndex) {

    $sentenceWords.css('width', 0);
    $(currentSentence).hide();

    loadSentence();

  } else {
    currentWordIndex++;
  }

}

function animateWords(word, callback) {
  var $word = $(word);
  var delay = $(word).data('delay') || standardDelay;
  var width = $(word).data('width') || '100%';
  var speed = $(word).data('speed') || animationDuration;

  $word.animate({
    width: width
  }, {
    duration: speed,

    complete: function() {

      if (typeof(callback) == 'function') {
        setTimeout(callback, delay);
      } else {
        setTimeout(loadWord, delay);
      }

    }
  });


}
loadSentence();
.element{
    text-align: left;
    color: white;
    background: gray;
    width: 120px;
    height: 600px;
    padding-top: 50pxs
}
.word-row{
  overflow: hidden;
  width: 0;
}
.word{
  display: block;
  width: 108px;
  white-space: nowrap;
  text-align: center;
  font-size: 22px;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="element typed-strings">
                


                <div class="sentence sentence-1">
                    <div class="word-row"><span class="word">First word</span></div>
                    <div class="word-row"><span class="word">Second word</span></div>
                    <div class="word-row"><span class="word"><strong>auto</strong></span></div>
                    <div class="word-row"><span class="word">strong</span></div>
                    <div class="word-row"><span class="word">super</span></div>
                    <div class="word-row"><span class="word">mario</span></div>
                </div>    

            
                <div class="sentence sentence-2">
                    <div class="word-row"><span class="word"><strong>hello</strong></span></div>
                    <div class="word-row"><span class="word"><strong>how is.</strong></span></div>
                    <div class="word-row" data-delay="1400"><span class="word"><strong>life</strong></span></div>
                    <div class="word-row"><span class="word"><strong>GREAT.</strong></span></div>
                </div>



                
            </div>
</body>
</html>

让我知道这是否适合你。