How to create typewriter effect with JavaScript
In this blog, I will show you how to create a typewriter effect with JavaScript.
Typewriter Effect Demo
Basic Idea
We will have an h1
tag. It will contain the text I am a
. Then we will have a word with the typewriter effect.
We will take each of the characters and put them in a span tag. Then we will put that span tag inside the h1
tag.
We will do this for each character. After completing the word, we will wait for a while and then start removing the character.
After removing the whole word, we will wait for a while and then start adding the next word.
This is how we will create the typewriter effect.
Html
1<body>2 <h1 class="text">I am a </h1>3</body>
a
is an HTML entity for non-breaking space.
Css
1@import url('https://fonts.googleapis.com/css2?family=Cousine:wght@700&display=swap');23* {4 padding: 0;5 margin: 0;6 box-sizing: border-box;7}89html {10 font-size: 62.5%;11}1213body {14 font-family: 'Cousine', monospace;15 min-height: 100vh;16 display: grid;17 place-items: center;18 background-color: black;19}2021:root {22 --text-color: white;23}2425.text {26 font-size: 4rem;27 display: inline-block;28 letter-spacing: 0.2rem;29 color: var(--text-color);30 border-right: 1rem solid var(--text-color);31 animation: blink 0.5s step-end infinite alternate;32}3334.char {35 color: orange;36}3738@keyframes blink {39 50% {40 border-color: transparent;41 }42}4344@media (min-width: 600px) {45 .text {46 font-size: 6rem;47 }48}4950@media (min-width: 900px) {51 .text {52 font-size: 8rem;53 }54}5556@media (min-width: 1200px) {57 .text {58 font-size: 11rem;59 }60}6162@media (min-width: 1600px) {63 .text {64 font-size: 14rem;65 }66}
Basic css for the website.
Javascript
1const textEl = document.querySelector('.text')23const words = ['Developer', 'Youtuber', 'Blogger'].map(word => word + '.')45let wordIndex = 06let charIndex = 078let addingChars = true9let shouldWait = false1011let currentWord = words[wordIndex]
Explanation:
textEl
is theh1
tag.words
is an array of words that will have the typewriter effect.wordIndex
is the index of the current word.charIndex
is the index of the current character.addingChars
is a boolean that tells us if we are adding characters or removing them.shouldWait
is a boolean that tells us if we should wait for a while.currentWord
is the current word that we are working on.
1const addChar = () => {2 let currChar = currentWord[charIndex]34 const char = document.createElement('span')56 char.innerText = currChar7 char.classList.add('char')89 textEl.appendChild(char)1011 charIndex++1213 if (charIndex === currentWord.length) {14 charIndex--15 addingChars = false16 shouldWait = true17 }18}
Explanation:
currChar
is the current character. We put them in a span tag and them inside thetextEl
element.- We increase the
charIndex
by 1. - If the
charIndex
is equal to the length of the current word, we decrementcharIndex
by 1. Because we have to start removing from the last character. - We set
addingChars
to false andshouldWait
to true. Because we have to wait for a while.
1const removeChar = () => {2 const char = textEl.lastElementChild34 textEl.removeChild(char)56 charIndex--78 if (charIndex < 0) {9 charIndex++10 addingChars = true11 updateCurrWord()12 }13}
Explanation :
- We remove the last child of the
textEl
element and decrease thecharIndex
by 1. - If the
charIndex
is less than 0, we increase it by 1. Because we have to start adding from the first character to the next word. - We set
addingChars
to true andshouldWait
to false. Because we have to add the next word. - And we call the
updateCurrWord
function. Let's implement theupdateCurrWord
function.
1const updateCurrWord = () => {2 wordIndex++34 if (wordIndex === words.length) wordIndex = 056 currentWord = words[wordIndex]7}
Explanation:
- We increase the
wordIndex
by 1. - If the
wordIndex
is equal to the length of the words array, we setwordIndex
to 0. Because we have to start from the first word. - Finally, we update the
currentWord
to the current index.
1const runTypewriter = () => {2 const operation = addingChars ? addChar : removeChar34 operation()56 let timeout = addingChars ? 200 : 10078 if (shouldWait) {9 timeout = 100010 shouldWait = false11 }1213 setTimeout(runTypewriter, timeout)14}1516setTimeout(runTypewriter, 1500)
Explanation:
- We create an
operation
variable. It will store the function that we want to call. It will be eitheraddChar
orremoveChar
depending on theaddingChars
variable. - We create a
timeout
variable. It will store the time that we want to wait. It will be either200
or1000
depending on theshouldWait
variable. - If
shouldWait
is true, we settimeout
to 1000 andshouldWait
to false. - We call the
runTypewriter
function inside thetypewriter
function. This is what we callrecursion
. But don't worry about it. It will create a loop that will create the typewriter effect. - Finally, we call the
runTypewriter
function globally after 1500 milliseconds.
Final Result
That's it! You can check out the GitHub repository for the code. Feel free to Star it, fork it, and make your own version.
Shameless Plug
I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: