Jquery code help typing text alone

Asked

Viewed 128 times

1

I found on the Internet this code to type a text alone by jquery, but I want to add a delay to it to just start typing when the page scrolls in the Section I want, how to add this function?

Code js

var div = document.getElementById('texto');
var texto = 'Porque Contratar a Two Way?';

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;
  }, 200);
}

escrever(texto, div);

The div is this:

<div id="texto"></div>

1 answer

2


When the div visible on screen, will call the function that writes:

var div = document.getElementById('texto');
var texto = 'Porque Contratar a Two Way?';
var flag = false; // flag para que função seja chamada apenas 1 vez

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;
  }, 200);
}

document.addEventListener("scroll", function(){
   var docScrl = document.documentElement.scrollTop, // pega o scroll da janela
       el = div.offsetTop, // distância da div ao topo
       winHeight = window.innerHeight; // altura da janela

   if(el-docScrl <= winHeight && !flag){
      flag = true;
      escrever(texto, div);
   }
});
role a janela para baixo
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<div id="texto"></div>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>

  • That’s what I wanted, thank you very much!

  • Why when entering two codes changing only the ID does not work? I created another div as text2 and inserted the code again and put in the id text2 but it doesn’t work.

  • I’ll see to it for you....

  • Basically you would have to create variables for each div, see: https://jsfiddle.net/foj26f8m/1/

  • Perfect bro, vlw

Browser other questions tagged

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