LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » How To Create A JavaScript Typewriter Using Pure Vanilla JavaScript?

How To Create A JavaScript Typewriter Using Pure Vanilla JavaScript?

How-To-Create-A-JavaScript-Typewriter-Using-Pure-Vanilla-JavaScript

You must have seen the Text Typewriter Effect on many website sliders or in a heading that looks like a full effect of typing animation character by character in typewriter effect. So we decided to make it a little snippet to let you use it on your web pages also to give your web page an awesome effect.

For programmer if we discuss this code in detail then We do not recommend it as it uses setTimeout(). Anything using setTimeout() or setInterval() slows down the page. Still, as it was a temporary landing page, this is how I approached it. The HTML structure is pretty simple and all details are contained into data parameters. I’d say it’s relatively safe for SEO.

There are many code snippets available online or on many other blogs and websites but everyone is not able to optimize your blog or website so you need some optimized code snippet. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready to use code and paste it where you want.

CLICK HERE TO SEE DEMO

Table of Contents

Features:

  1. Awesome Typewriter Effect.
  2. Static Prefix With Changeable Text.
  3. Smorrt Typewriter Style Animationt.
  4. Easy To Use.
  5. Chracter By Chracter Tyoing Effect.
  6. Pure Vanilla JavaScript with CSS.
  7. No External Library or JQuery.
  8. Full Customizable Code

How To Create A JavaScript Typewriter Using Pure Vanilla JavaScript?

There are few easy and understandable steps to achieve your desired functionality using pure Vanilla JavaScript that we are gonna share below. Follow each step perfectly.

CSS:

.header-text {
font-family: "Poppins";
font-weight: 500;
}

.typewrite > .wrap {
position: relative;
font-weight: 300;
}
.typewrite > .wrap:after {
content: "?";
color: orange;
font-weight: 500;

position: absolute;
animation: blinkerText 1s linear infinite;
}

@keyframes blinkerText {
50% {
opacity: 0;
}
}

JavaScript:

let TxtType = function (el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = "";
this.tick();
this.isDeleting = false;
};

TxtType.prototype.tick = function () {
let i = this.loopNum % this.toRotate.length;
let fullTxt = this.toRotate[i];

if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}

this.el.innerHTML = `<span class="wrap">${this.txt}</span>`;

let that = this;
let delta = 100 - Math.random() * 100;
if (this.isDeleting) {
delta /= 4;
}

if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === "") {
this.isDeleting = false;
this.loopNum++;
delta = 1000 / 60;
}

setTimeout(() => {
that.tick();
}, delta);
};

document.addEventListener('DOMContentLoaded', () => {
let elements = document.getElementsByClassName('typewrite');
for (let i = 0; i < elements.length; i++) {
let toRotate = elements[i].dataset.type;
let period = elements[i].dataset.period;

if (toRotate) {
new TxtType(elements[i], JSON.parse(toRotate), period);
}
}
});

HTML:

<h2 class="header-text">
roo: <span class="typewrite" data-period="2000" data-type='[ "Is Your Site Slow", "Has Your Site Been Hacked", "Do You Need Website Maintenance", "Do You Want Better Page Speed" ]'>
<span class="wrap"></span>
</span>
</h2>

Customization:

Feel free to edit that code as it is self-explanatory. Rest we are here for you.

Recommended For You:
Zoom Image In Circle On Mouseover Using Pure JavaScript

Troubleshooting the Errors

Do it with concentration and patience. Check your alls steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us or If you have any doubts and problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

You Like It, Please Share This Recipe With Your Friends Using...

2 Responses to “How To Create A JavaScript Typewriter Using Pure Vanilla JavaScript?”

  1. Ridzi Arora says:

    I found your article really helpful. I appreciate your efforts and looking forward to more articles from you.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you more awesome and valuable content from a different mind. Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *