JS Typewriter Effect Multiple Lines

JS Typewriter Effect Multiple Lines
Code Snippet:Simple Typing text animation Javascript
Author: Laith Haleem
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 5,911
License: MIT
Edit Code online: View on CodePen
Read More

This JS code snippet helps you to create typewriter effect with multiple lines. It uses JavaScript string split and timeout function to animate characters to make a typing effect.

You can set a longer string value in its variable that you want to display as typewriter effect. This code snippet magically split lengthy string to multiple lines and animates each character with typing effect.

How to Create JS Typewriter Effect Multiple Lines

1. In order to create typewriter animated text, you just need a container element in which the text will be rendered dynamically. So, create a div element and define its id “str” as follows:

 <div id="str"></div>

2. After that, add the following CSS styles to style the text container. Basically, these styles are optional, you can define your own CSS according to your needs.

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}


#str {
   width: 100%;
   padding: 20px;
   height: auto;
   font-family: sans-serif;
   font-size: 40px;
   margin: 20px auto;
   color: #fff;
   font-weight: bold;

}

3. Finally, add the following JavaScript typewriter effect function between the <script> and </script> before closing the body tag. You just need to replace the value of “string” variable with your own text that you want to display with typing effect.

var string = "CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts.";
var str = string.split("");
var el = document.getElementById('str');
(function animate() {
str.length > 0 ? el.innerHTML += str.shift() : clearTimeout(running); 
var running = setTimeout(animate, 90);
})();

That’s all! hopefully, you have successfully integrated this JS typewriter effect with multiple lines code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

3 thoughts on “JS Typewriter Effect Multiple Lines”

  1. It does works while using it for a Single element, however when i tried using same code for 2 diff elements it was giving some runtime error, Edit : It got fixed after i changed var name !

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...