Typewriter effect

by | Feb 20, 2019 | Web Design, Demo

To be successful is to
_
Typewriter effect can be achieved in many different ways but here is one example of a typewriter effect using HTML, CSS and JavaScript.

HTML code example

<div class=”console-container”>
To be
<span class=”highlight”> successful</span>
is to
<span id=”text”></span>
<div class=”console-underscore” id=”console”>_</div>
</div>

In HTML code static part of the text can be defined. Div with the ID text is used to insert dynamic part of the sentence. That part if the text is inserted using Java Script. In CSS code, text can be customized. Define color, size and position of the text. In JavaScript you can add dynamic part of the sentence that will be inserted into HTML. That text is being parsed and inserted each character at a time. Speed of insertion can also be adjusted inside JavaScript code.

CSS code example

.hidden {
 opacity:0;
}
.console-container {
 color: #fff;
 text-shadow:2px 2px 2px #333;
 font-size:3em;
 text-align: left;
 height:30px;
 display:block;
 position:relative;
 color:black;
 top:0;
 bottom:0;
 left:5rem;
 right:0;
 margin:auto;
font-weight: 500;
}
.console-underscore {
 display:inline-block;
 position:relative;
 left:10px;
}
.highlight{
 font-size: 4.5rem;
 color: orange;
 display:block;
 margin:15px 0;
 text-transform:uppercase;
 font-weight:bold; 
 line-height:1;
}

@media (max-width: 750px) {
 .console-container { font-size:1em; }
}

JavaScript code example

 consoleText(['trust yourself!', 'break some rules!', 'not be afraid to fail!', 'ignore the naysayers!', 'work like hell!', 'give something back!'], 'text',['#fff']);

function consoleText(words, id, colors) {
 if (colors === undefined) colors = ['#fff'];
 var visible = true;
 var con = document.getElementById('console');
 var letterCount = 1;
 var x = 1;
 var waiting = false;
 var target = document.getElementById(id)
 target.setAttribute('style', 'color:' + colors[0])
 window.setInterval(function() {

 if (letterCount === 0 && waiting === false) {
 waiting = true;
 target.innerHTML = words[0].substring(0, letterCount)
 window.setTimeout(function() {
 var usedColor = colors.shift();
 colors.push(usedColor);
 var usedWord = words.shift();
 words.push(usedWord);
 x = 1;
 target.setAttribute('style', 'color:' + colors[0])
 letterCount += x;
 waiting = false;
 }, 1000)
 } else if (letterCount === words[0].length + 1 && waiting === false) {
 waiting = true;
 window.setTimeout(function() {
 x = -1;
 letterCount += x;
 waiting = false;
 }, 1000)
 } else if (waiting === false) {
 target.innerHTML = words[0].substring(0, letterCount)
 letterCount += x;
 }
 }, 60)
 window.setInterval(function() {
 if (visible === true) {
 con.className = 'console-underscore hidden'
 visible = false;

 } else {
 con.className = 'console-underscore'

 visible = true;
 }
 }, 400)
}
  • David M. Mroz

    Dave is the founder of Glimmernet Technologies, where he’s spent over two decades helping organizations navigate the complex world of web development, cybersecurity, and custom software. Equal parts strategist and engineer, David brings a sharp eye for practical security, clean code, and content that doesn’t waste your time. When he’s not advising clients or exposing online scams, he’s probably knee-deep in a side project involving WordPress, APIs, or a creative automation workaround nobody asked for (but everyone needed).

2 Comments

  1. Larry Roberts

    Love your code, I hope to try it out soon. However, successful is spelled wrong. It has 2 c’s.

    Reply
    • Dave Mroz

      Good catch! We fixed it!

      Reply

Submit a Comment

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


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