【问题标题】:Typing effect: Word per Word [closed]打字效果:Word per Word [关闭]
【发布时间】:2017-07-06 20:35:14
【问题描述】:

我一直在研究如何获得一个单词一个单词而不是一个字母一个字母的打字效果。到目前为止,我搜索过的所有链接都提供逐字母打字机效果。

https://macarthur.me/typeit/ https://github.com/mattboldt/typed.js/

这有可能实现吗?或者你做过类似的事情吗?

【问题讨论】:

标签: javascript jquery css


【解决方案1】:

这很容易。 只需注册一个间隔,拆分您的文本,反转数组并弹出最后一项。然后附加到文本容器的那个。完成。

JS

var myText = "Some text you want to be typed automagically..";
var myWords = myText.split(" ").reverse();

var ntrvl = setInterval(function() {
  addNextWord();
}, 150);

function addNextWord() {
  var nextWord = myWords.pop();
  if(nextWord !== undefined) {
      var textNow = $(".write-here").text() + " " + nextWord;
      $(".write-here").text(textNow);
  }
}

你怎么看?

JSfiddle

【讨论】:

  • 非常感谢! :) 他们说这不是要解决的特定程序……但是您仍然提供了帮助!对此,我真的非常感激。 :)
  • @Woppi 你甚至改进了代码,这很好。干杯!
【解决方案2】:

使用您想要一次打印的任何单词创建一个数组。

const sentence = 'this sentence will be displayed one word at a time';
var arrayOfWords = sentence.split(' ');

for (var i = sentence.length - 1; i >= 0; i--) {
    setTimeout(function(){
        console.log(sentence[i]); // or display another way
    }, 400) // set this to your desired interval
}

【讨论】:

  • 你也可以创建一个字符串并让函数拆分它..
  • @PatrickMlr 是的,你是对的。使用 split 方法会更方便,而不是手动拆分输入
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 2016-10-16
相关资源
最近更新 更多