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.
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.
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 !
Many many thanks to the coder for making such easy code <3 !
Hi Rachit,
You’re welcome! keep visiting us for more free web design code snippets.