Hello Guys! Welcome to Coding Torque. In this blog, I’m going to explain to you how to make Typing effect using vanilla JavaScript. You can use this effect on your website/portfolio to showcase any skills/features. This will be a step-by-step guide. Let’s get started 🚀.
Before we start, here are some JavaScript Games you might like to create:
1. Snake Game using JavaScript
2. 2D Bouncing Ball Game using JavaScript
3. Rock Paper Scissor Game using JavaScript
4. Tic Tac Toe Game using JavaScript
5. Whack a Mole Game using JavaScript
I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.
HTML CodeÂ
Starter Template
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Typing Effect using JavaScript - @code.scientist</title> </head> <body> <!-- Further code here --> </body> </html>
Paste the below code in your <body>
tag
<div class="container"> <p>Coding is <span class="typed-text"></span><span class="cursor"> </span></p> </div>
Output Till Now
CSS CodeÂ
Create a file style.css
and paste the code below.
@import url('https://fonts.googleapis.com/css?family=Montserrat'); * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Montserrat', sans-serif; background-color: #000; color: #eee; } .container { height: 100vh; display: flex; justify-content: center; align-items: center; } .container p { font-size: 3rem; padding: 0.5rem; font-weight: bold; letter-spacing: 0.1rem; text-align: center; overflow: hidden; } .container p span.typed-text { font-weight: normal; color: #dd7732; } .container p span.cursor { display: inline-block; background-color: #ccc; margin-left: 0.1rem; width: 3px; animation: blink 1s infinite; } .container p span.cursor.typing { animation: none; } @keyframes blink { 0% { background-color: #ccc; } 49% { background-color: #ccc; } 50% { background-color: transparent; } 99% { background-color: transparent; } 100% { background-color: #ccc; } }
Output Till Now
JavaScript CodeÂ
Create a fileÂ
script.js
 and paste the code below.const typedTextSpan = document.querySelector(".typed-text"); const cursorSpan = document.querySelector(".cursor"); const textArray = ["hard", "fun", "a journey", "LIFE"]; const typingDelay = 200; const erasingDelay = 100; const newTextDelay = 2000; // Delay between current and next text let textArrayIndex = 0; let charIndex = 0; function type() { if (charIndex < textArray[textArrayIndex].length) { if (!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing"); typedTextSpan.textContent += textArray[textArrayIndex].charAt(charIndex); charIndex++; setTimeout(type, typingDelay); } else { cursorSpan.classList.remove("typing"); setTimeout(erase, newTextDelay); } } function erase() { if (charIndex > 0) { if (!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing"); typedTextSpan.textContent = textArray[textArrayIndex].substring(0, charIndex - 1); charIndex--; setTimeout(erase, erasingDelay); } else { cursorSpan.classList.remove("typing"); textArrayIndex++; if (textArrayIndex >= textArray.length) textArrayIndex = 0; setTimeout(type, typingDelay + 1100); } } document.addEventListener("DOMContentLoaded", function () { // On DOM Load initiate the effect if (textArray.length) setTimeout(type, newTextDelay + 250); });
Written by: Piyush Patil
Code Credits:Â @Coding_Journey
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpful💖