Elevate your website's interactivity with the captivating auto-type text effect. In this tutorial, we'll guide you through the process of creating a seamless and attention-grabbing auto-typing text using HTML, CSS, and JavaScript. Learn how to bring a dynamic element to your website and engage your audience with this trendy and user-friendly feature.
Step 1: HTML Markup:
Begin by setting up the HTML structure. Create a container to hold the auto-typing text, giving your users a focused and aesthetically pleasing reading experience.
<!-- -------------------- HTML -------------------- -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Auto Typing and Untyping Text</title>
<!-- Link CSS -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">I'm a <span id="text"></span></div>
<!-- Link Script -->
<script src="main.js"></script>
</body>
</html>
Step 2: Styling with CSS:
Create a separate CSS file (style.css)
/*-------------------- CSS --------------------*/
body{
height: 100vh;
background: #022d45;
display: flex;
align-items: center;
justify-content: center;
font-family: sans-serif;
}
.container{
width: 70%;
color: aliceblue;
font-size: 1.5rem;
}
#text{
font-family: Arial, Helvetica, sans-serif;
font-size: 2rem;
border-right: 2px solid aliceblue;
padding-right: 2px;
border-bottom: 1px solid aliceblue;
}
Step 3: Adding JavaScript Interaction:
Leverage JavaScript to orchestrate the auto-typing effect. Customize the animation speed and style to suit your website's vibe.
//-------------------- Java Script --------------------//
let textElement = document.getElementById("text");
let textArray = ["Programmer", "Graphic Designer", "Youtuber", "Web Developer"];
let textIndex = Math.floor(Math.random() * textArray.length);
let currentText = "";
let textPosition = 0;
let textColor = ["#ff0000", "#00ff00", "##009dff", "#ff00ff", "#00ffff"];
textElement.style.color = textColor[Math.floor(Math.random() * textColor.length )]
function textType(){
if(textPosition < textArray[textIndex].length){
currentText += textArray[textIndex].charAt(textPosition);
textElement.innerHTML = currentText;
textPosition++;
setTimeout(textType, 100);
}
else{
setTimeout(eraseText, 1500);
}
}
function eraseText(){
if(currentText.length > 0){
currentText = currentText.slice(0, -1);
textElement.innerHTML = currentText;
setTimeout(eraseText, 100);
}
else{
textIndex ++;
if(textIndex >= textArray.length){
textIndex = 0;
}
textPosition = 0;
currentText = "";
textElement.style.color = textColor[Math.floor(Math.random() * textColor.length )]
setTimeout(textType, 100)
}
}
textType()
Tailor the text content, styling, and animation speed to match your website's theme. Consider integrating this feature in your hero section, landing pages, or as a unique element in your webpage.
0 Comments