This JavaScript code snippet helps you to create a typewriter text effect with a blinking cursor. It gets text strings from the array and applies a key hitting effect on each letter. You can also pass the text array through the data attribute on which you want to apply typing effect.
How to Create Typewriter Text Effect JavaScript
1. First of all, create a span element with the id “typewriter” and define its data-array attribute (or leave it if you want to define it in the JS program). Similarly, create a span element with a class name “cursor” that will show as a cursor. Wrap all these elements into a wrapper element.
<div class="wrapper"> <div class="centeredBox"> <span id="typewriter" data-array=""></span> <span class="cursor"></span> </div> </div>
2. After that, define the CSS style for the typewriter as follows:
/* Wrapper to Center the Typewriter */ .wrapper { background: #149cbe; color: #fff; font-family: "Roboto", sans-serif; font-size: 60px; width: 100%; height: 420px; display: flex; justify-content: center; align-items: center; } .centeredBox { width: 100%; text-align: center; } /* The Typewriter */ #typewriter{ } .cursor { border-left: 4px solid white; }
3. Finally, add the following JavaScript code and done.
// The typewriter element var typeWriterElement = document.getElementById('typewriter'); // The TextArray: var textArray = ["Hey, I'm Tim.","I like JavaScript.","I Love to Develop.", "I like this Typewriter."]; // You can also do this by transfering it through a data-attribute // var textArray = typeWriterElement.getAttribute('data-array'); // function to generate the backspace effect function delWriter(text, i, cb) { if (i >= 0 ) { typeWriterElement.innerHTML = text.substring(0, i--); // generate a random Number to emulate backspace hitting. var rndBack = 10 + Math.random() * 100; setTimeout(function() { delWriter(text, i, cb); },rndBack); } else if (typeof cb == 'function') { setTimeout(cb,1000); } }; // function to generate the keyhitting effect function typeWriter(text, i, cb) { if ( i < text.length+1 ) { typeWriterElement.innerHTML = text.substring(0, i++); // generate a random Number to emulate Typing on the Keyboard. var rndTyping = 250 - Math.random() * 100; setTimeout( function () { typeWriter(text, i++, cb) },rndTyping); } else if (i === text.length+1) { setTimeout( function () { delWriter(text, i, cb) },1000); } }; // the main writer function function StartWriter(i) { if (typeof textArray[i] == "undefined") { setTimeout( function () { StartWriter(0) },1000); } else if(i < textArray[i].length+1) { typeWriter(textArray[i], 0, function ()Â { StartWriter(i+1); }); } }; // wait one second then start the typewriter setTimeout( function () { StartWriter(0); },1000);
That’s all! hopefully, you have successfully integrated this typewriter text effect code snippet into your project. If you have any questions or facing any issues, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.
The code is really good. In the javascript attached, In the main typewriter function, in the callback function ‘A’ letter has to be removed for it to work properly