简体   繁体   English

连续显示文本

[英]Reveal text in succession

How can I use JavaScript/jQuery to get text to appear in succession? 如何使用JavaScript / jQuery连续显示文本?

I want a half second between each character, and then I want to repeat the same succession 2-3 times. 我希望每个角色之间有半秒钟,然后我想重复相同的连续2-3次。 I am coming up dry on all of my web searches for this. 在我的所有网络搜索中,我都是干涸的。

jsFiddle Demo

Use timeouts with callbacks. 使用带有回调的超时。 You can do this natively with javascript using setTimeout , or with jQuery using delay . 您可以使用setTimeout使用javascript本地执行此操作,或使用delay使用jQuery执行此操作。 In order to facilitate the repetition, you could use a recursive loop with a counter for completion in addition to current text offset. 为了便于重复,除了当前文本偏移之外,还可以使用带计数器的递归循环来完成。

html for demo 用于演示的HTML

<div id="d"></div>

js for demo 用于演示的js

var text = "Hello World!";
var d = $("#d")[0];
(function write(i,n){
 if( i == text.length ){
  if( n == 2 ) return;
  d.innerHTML = "";
  setTimeout(function(){write(0,++n)},500);
 }else{
  d.innerHTML += text[i++];
  setTimeout(function(){write(i,n)},500);
 }
})(0,0)

Here is a slightly more in depth demo which uses a block of text (the writing is sped up for proof of concept). 这是一个稍微深入一点的演示,它使用了一个文本块(加快了写作以进行概念验证)。 http://jsfiddle.net/64uNd/ http://jsfiddle.net/64uNd/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM