Text that you type little by little?

Asked

Viewed 11,586 times

10

Guys, I see an effect on the sites. The Text will appear (as if someone were typing it). Automatically I related to Mom, but the effect is not cool. You know how to do?

  • If you are using jQuery, there is such a plugin as to facilitate the development of your need. http://www.mattboldt.com/demos/typed-js/

4 answers

22

This is +/- simple. An example would be like this:

var div = document.getElementById('log');
var texto = 'Hoje está um lindo dia!';

function escrever(str, el) {
  var char = str.split('').reverse();
  var typer = setInterval(function() {
    if (!char.length) return clearInterval(typer);
    var next = char.pop();
    el.innerHTML += next;
  }, 100);
}

escrever(texto, div);
<div id="log"></div>

From a text string it breaks the string into an array, character by character and inverts the order (to use the .pop).

Then using the setInterval and .pop() Let’s empty this array with text.

jsFiddle: http://jsfiddle.net/yLy0jxeb/1/

  • 2

    the most beautiful of this code is the clearInterval ;D

6

Alternative:

var texto = "Hello World!!!";
var result;

var count = 0;
function digitar() {
  result = document.getElementById("result");
  window.setTimeout(function() { inserir(texto[count]) }, 200);
}

function inserir(letra) {
  result.innerHTML += letra;
  count++;
  if(count < texto.length)
    window.setTimeout(function() { inserir(texto[count]) }, 200);
}

window.onload = digitar;
<div id="result"></div>

6

Alternative to the colleague’s code [Sergio], so only to put everything in a function, without having global variables (which will hardly be used by other functions)

function typeWritter(texto,idElemento,tempo){
    var char = texto.split('').reverse();
    var typer = setInterval(function () {
        if (!char.length) return clearInterval(typer);
        var next = char.pop();
        document.getElementById(idElemento).innerHTML += next;
    }, tempo);
}
typeWritter('Hoje está um lindo dia!','log',100);

function typeWritter(texto,idElemento,tempo){
    var char = texto.split('').reverse();
    var typer = setInterval(function () {
        if (!char.length) return clearInterval(typer);
        var next = char.pop();
        document.getElementById(idElemento).innerHTML += next;
    }, tempo);
}
typeWritter('Hoje está um lindo dia!','log',100);
<div id="log"></div>

Jsfiddle http://jsfiddle.net/yLy0jxeb/15/

  • 1

    Hi Rodrigo! Just now I saw your answer :) When you say "put everything into one function, without having global variables" That’s what I did... There are no global variables in my code and it’s all in one function... you can explain better the difference you point out?

  • The variables you put as global were div and text, in the first two lines of your code. What I did was put them inside the function. The intention was just to show the use without them and passing the id of the element instead of the element itself. It is a mere alternative, your code is correct. : D

  • I had forgotten, I put a parameter for the typing time.

3

An alternative to [Sergio] code using an anonymous function:

(function() {
  var node = document.getElementById('message'),
    message = "This is my message... ! Thx =)",
    current = message.split("").reverse(),
    interval = setInterval(function() {
      if (current.length)
        node.innerHTML += current.pop();
      else
        clearInterval(interval);
    }, 300);
}());
<div id="message"></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.