Skip to content

Instantly share code, notes, and snippets.

@HeroDjou
Last active December 21, 2023 05:25
Show Gist options
  • Save HeroDjou/58c0846740f73f56e6ebec39733f2f40 to your computer and use it in GitHub Desktop.
Save HeroDjou/58c0846740f73f56e6ebec39733f2f40 to your computer and use it in GitHub Desktop.
Simple typewriting effect
<!-- Example of usage -->
<span id="escreveTexto"></span>
const palavras = ['text 1', 'text 2', 'text 3', 'text 4'];
const textoElement = document.getElementById('escreveTexto');
let index = 0;
let isDeleting = false;
let textoAtual = '';
let velocidadeEscrita = 100; // Typing speed
let velocidadeApagar = 35; // Erasing speed
function trocarTexto() {
const palavraAtual = palavras[index];
if (!isDeleting) {
textoAtual = palavraAtual.substring(0, textoAtual.length + 1);
textoElement.textContent = 'This is my ' + textoAtual;
if (textoAtual === palavraAtual) {
isDeleting = true;
setTimeout(trocarTexto, 1500); // Time to initiate "erase"
} else {
setTimeout(trocarTexto, velocidadeEscrita);
}
} else {
textoAtual = palavraAtual.substring(0, textoAtual.length - 1);
textoElement.textContent = 'Claudio Fernandes é ' + textoAtual;
if (textoAtual === '') {
isDeleting = false;
index = (index + 1) % palavras.length;
}
setTimeout(trocarTexto, velocidadeApagar);
}
}
setTimeout(trocarTexto, 2000); // Start after 2 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment