Created
May 30, 2020 12:20
-
-
Save AlphaT7/551f8a77dac9847e3c0f17d814b522ef to your computer and use it in GitHub Desktop.
JS Typing Animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Typing Animation</h1> | |
<p> | |
<!-- In order for the curser to stay on the same line as the typing text, the typing text element must be inside of a <p> tag --> | |
<span id="demo" class="typing cursor"></span> | |
</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.addEventListener("load", () => { | |
document.querySelector("#demo").innerHTML = ""; | |
let i = 0; | |
let txt = | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc id dapibus nibh. Aenean consectetur turpis eget leo laoreet, et porttitor orci hendrerit."; | |
let speed = 50; | |
type(txt, i, speed); | |
}); | |
let type = (txt, i, speed) => { | |
if (i < txt.length) { | |
document.querySelector("#demo").classList.remove("typing"); | |
document.getElementById("demo").innerHTML += txt.charAt(i); | |
i++; | |
setTimeout(() => { | |
type(txt, i, speed); | |
}, speed); | |
} else { | |
document.querySelector("#demo").classList.add("typing"); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background: #000; | |
} | |
div { | |
margin: 0 auto 20px auto; | |
} | |
p { | |
color: #fff; | |
width: 400px; | |
margin: 0 auto 0 auto; | |
} | |
.cursor { | |
border-right: 0.2em solid #fff; | |
} | |
.typing { | |
animation: blink-caret 0.75s step-end infinite; | |
} | |
/* The typewriter cursor effect */ | |
@keyframes blink-caret { | |
from, | |
to { | |
border-color: transparent; | |
} | |
50% { | |
border-color: #fff; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment