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 | Section B | Extra |
Closing |
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
Basic Javascript Typewriter Demo
All you need on the HTML side is a container Yep, that’s all to the typewriter effect: 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. The HTML of this improved version is just a tad bit different, as in, we put in an additional “fake cursor” 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. The basic mechanics behind the Javascript is actually still the basic, but with small additions: That’s all for the code, and here is the download link as promised – Plus a small demo of the final power-up version. 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) 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. 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.THE JAVASCRIPT
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
var tw
object to hold all the typewriter-related properties and functions.container
Which HTML element to display the typewriter text in.text
The text to display.delay
Time delay between displaying each character in microseconds.timer
To hold the Javascript timer that will time the text display.pointer
The current position of the text.draw
that will automatically loop through the text
and display it character by character.SECTION B
POWER UP!STEP 1) THE HTML
at the end of the text to spice up the visuals a little.
STEP 2) THE 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;
}
STEP 3) THE JAVASCRIPT
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
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 & DEMOTHE DEMO
NEWS TICKER
SOURCE CODE DOWNLOAD
CLOSING
WHAT’S NEXT?