Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

3 Steps Simple Pure Javascript Typewriter Effect

INTRODUCTION
EASY EYE CANDY

Welcome to a tutorial on how to create a pure Javascript Typewriter effect. Want to add some visual eye candy to your website headlines? The typewriter effect will sure catch some attention, and it is actually ridiculously easy to do. Read on to find out!

I have included a zip file with all the source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!


 

NAVIGATION
TABLE OF CONTENTS

Section A
The Basics

Section B
Power Up

Extra
Download & Demo

Closing
What’s Next?

SECTION A
THE BASIC VERSION

Let us start with the raw basic version – This one will walk you through the mechanics behind the typewriter effect, plain and vanilla without a single line of CSS.

THE HTML

1-basic.html


  
    
      Basic Javascript Typewriter Demo
    

All you need on the HTML side is a container 

 to display the text.

THE JAVASCRIPT

1-basic.js
var tw = {
  /* [THE SETTINGS] */
  container: "twrap", // ID of typewriter container
  text: "Wow. Much text. Very paragraph. Such contents.", // Text to show
  delay: 200, // Delay in between each character

  /* [THE MECHANICS] */
  timer: null, // Used to hold the timer
  pointer: 0, // Current text position
  draw : function (){
  // tw.draw() : typewriter effect

    tw.pointer++;
    tw.container.innerHTML = tw.text.substring(0, tw.pointer);
    if (tw.pointer 

Yep, that’s all to the typewriter effect:

  • We begin by creating a var tw object to hold all the typewriter-related properties and functions.
  • That includes the settings:
    • container Which HTML element to display the typewriter text in.
    • text The text to display.
    • delay Time delay between displaying each character in microseconds.
  • The required players:
    • timer To hold the Javascript timer that will time the text display.
    • pointer The current position of the text.
  • Finally, as the window is fully loaded, we fire up the function draw  that will automatically loop through the text and display it character by character.

SECTION B
POWER UP!

The basic version is nice, but it still lacks some “visual power”. So in section, we beef up the basic version and give it some muscles.

STEP 1) THE HTML

2-power.html


  
    
      Javascript Typewriter Demo
    
|

The HTML of this improved version is just a tad bit different, as in, we put in an additional “fake cursor”  at the end of the text to spice up the visuals a little.

STEP 2) THE CSS

2-power.css
/* [TYPEWRITER] */
#twrapOut {
  box-sizing: border-box;
  width: 100%;
  height: 100vh;
  padding-top: 30px;
  background: #e00;
  color: #fff;
  text-align: center;
  font-family: arial;
  font-size: 2em;
}
@keyframes blink {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
#cursor {
  animation: blink 0.8s infinite;
}

/* [DOES NOT MATTER] */
html, body {
  padding: 0;
  margin: 0;
}

Just some cosmetics – Messing around with the typewriter container, and to create a blinking cursor with an infinite opacity animation loop. Feel free to change the styles to fit your own project.

STEP 3) THE JAVASCRIPT

2-power.js
var tw = {
  /* [THE SETTINGS] */
  container: "twrapIn", // ID of typewriter container
  text: [ // Blocks of text to show
    "Wow. Much text. Very paragraph. Such contents.",
    "The disregard exits on top of the medium.",
    "Can the damaged key pose as a deal automobile?"
  ], 
  delay: 100, // Delay in between each character
  blockDelay: 800, // Delay in between each block of text

  /* [THE MECHANICS] */
  timer: null, // Used to hold the timer
  pointer: 0, // Current text position
  block: 0, // Current block of text
  draw : function () {
  // tw.draw() : typewriter effect

    // Draw next character
    tw.pointer++;
    tw.container.innerHTML = tw.text[tw.block].substring(0, tw.pointer);
    if (tw.pointer 

The basic mechanics behind the Javascript is actually still the basic, but with small additions:

  • Instead of displaying a single sentence text, we declare an array that can hold many different sentences of text.
  • draw does the same thing to display character-by-character, but when it is done, reset will fire up and move on to display the next block of text.

EXTRA
DOWNLOAD & DEMO

That’s all for the code, and here is the download link as promised – Plus a small demo of the final power-up version.

THE DEMO

|

NEWS TICKER

Need more text effects? Check out my other tutorial on how to create news tickers:

2 Ways to Create HTML & CSS News Ticker (Horizontal and Vertical)

SOURCE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
 

CLOSING
WHAT’S NEXT?

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to create more visually appealing elements in your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

The post 3 Steps Simple Pure Javascript Typewriter Effect appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

3 Steps Simple Pure Javascript Typewriter Effect

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×