Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="cbwrap">
  <!-- (X) TITLE -->
  <h1 id="cbtitle">Javascript Typewriter</h1>
  
  <!-- (A) THE PROPER DEMO -->
  <div id="demo"></div>
  Theme: <select id="theme">
    <option value="">None</option>
    <option value="retro">Retro</option>
    <option value="impact">Impact</option>
    <option value="rose">Rose</option>
    <option value="code">Code</option>
    <option value="banana">Banana</option>
  </select>
  
  <!-- (X) INFO SNIPPET -->
  <div id="cbinfo">
    Visit <a href="https://code-boxx.com/simple-javascript-typewriter-effect/" target="_blank">Code Boxx</a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) BLINKING CURSOR EFFECT */
@keyframes blink {
  0% { opacity: 0; }
  100% { opacity: 1; }
}
.cursor::after {
  content: "|";
  font-size: 1.5em;
  font-weight: bold;
  animation: blink infinite alternate 0.4s;
}

/* (B) "THEMES" */
.retro {
  font-family: Courier, monospace;
  font-weight: 700;
  color: #333;
  background: #d7ffe7;
  padding: 5px;
}
.impact {
  font-family: Impact, sans-serif;
  text-transform: uppercase;
  color: #fff;
  background: #993535;
  padding: 5px;
}
.rose {
  font-family: 'Brush Script MT', cursive;
  color: #a52e2e;
  background: #ffeeee;
  padding: 5px;
}
.code {
  font-family: "Lucida Console", monospace;
  color: #0dff00;
  background: #141414;
  padding: 5px;
}
.banana {
  font-family: "Arial Black",sans-serif;
  color: #83700e;
  background: #f9ff00;
  padding: 5px;
}

/* (X) DOES NOT MATTER */
/* (X1) LAYOUT COSMETICS */
* { box-sizing: border-box; }
body {
  font-family: arial, sans-serif;
  padding: 0; margin: 0; border: 0;
  min-height: 100vh;
  display: flex; justify-content: center; align-items: center;
  background: url(https://images.unsplash.com/photo-1520096021341-b6742a955b1f?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2ODg3OTQ2OTl8&ixlib=rb-4.0.3&q=85);
  background-size: cover;
  background-position: center;
  backdrop-filter: blur(10px);
}
#cbwrap {
  background: rgba(255, 255, 255, 0.9);
  width: 600px; padding: 20px;
  border-radius: 10px;
}
#cbtitle {
  margin-bottom: 40px;
  text-transform: uppercase;
}
#cbinfo {
  padding: 10px; margin-top: 40px;
  font-weight: 700; text-align: center;
}
#cbinfo a {
  text-decoration: none; padding: 5px;
  color: #fff; background: #a91616;
}

/* (X2) FORM COSMETICS */
select {
  padding: 5px;
  margin-top: 10px;
}
              
            
!

JS

              
                function tw (instance) {
  // (A) SET DEFAULT OPTIONS
  if (instance.forward === undefined) { instance.forward = 100; }
  if (instance.backward === undefined) { instance.backward = 50; }
  if (instance.pause === undefined) { instance.pause = 1000; }
  if (instance.loop === undefined) { instance.loop = true; }
  if (instance.cursor === undefined) { instance.cursor = true; }
  if (typeof instance.text != "object") { instance.text = [instance.text]; }

  // (B) PROPERTIES & FLAGS
  instance.current = 0;      // current block of text
  instance.char = 0;         // current character
  instance.direction = true; // true forward, false backward
  instance.draw = true;      // continue to "type text"?

  // (C) ATTACH FAKE CURSOR
  if (instance.cursor) { instance.target.classList.add("cursor"); }

  // (D) TYPEWRITER EFFECT
  instance.typist = () => {
    // (D1) NEXT CHARACTER
    if (instance.direction) {
      instance.char++;
      instance.draw = instance.char <= instance.text[instance.current].length;
    } else {
      instance.char--;
      instance.draw = instance.char >= 0;
    }

    // (D2) DRAW HTML
    if (instance.draw) {
      instance.target.innerHTML = instance.char==0 ? "&nbsp;" : instance.text[instance.current].substring(0, instance.char);
    }

    // (D3) PAUSE & LOOP?
    else {
      // (D3-1) CLEAR TIMER + REVERSE DIRECTION
      clearInterval(instance.timer);
      instance.direction = !instance.direction;

      // (D3-2) NEXT BLOCK OF TEXT
      if (instance.direction) {
        instance.current++;
        if (instance.loop && instance.current == instance.text.length) {
          instance.current = 0;
        }

        if (instance.current <= instance.text.length) {
          instance.timer = setTimeout(() => {
            instance.timer = setInterval(instance.typist, instance.forward);
          }, instance.pause);
        }
      }

      // (D3-3) PAUSE THEN CLEAR TEXT
      else {
        instance.timer = setTimeout(() => {
          instance.timer = setInterval(instance.typist, instance.backward);
        }, instance.pause);
      }
    }
  };

  // (E) START
  instance.timer = setInterval(instance.typist, instance.forward);
}

// (F) ATTACH TYPEWRITER & THEME SWITCHER
window.onload = () => {
  // (F1) GET DEMO <DIV>
  var demo = document.getElementById("demo");

  // (F2) THEME SWITCHER
  document.getElementById("theme").onchange = function () {
    let cur = demo.classList.contains("cursor");
    demo.className = this.value;
    if (cur) { demo.classList.add("cursor"); }
  };

  // (F3) ATTACH TYPEWRITER
  tw({
    // (F3-1) REQUIRED
    target : demo,
    text : [
      "Wow. Much text. Very paragraph. Such contents.",
      "Foo! It works!",
      "Another random paragraph here."
    ],
   
    // (F3-2) OPTIONAL
    forward : 80,  // delay between each character, default 100 ms
    backward : 40, // delay between each character, default 50 ms
    pause : 1500,  // pause before next block of text, default 1 sec
    loop : true,   // loop text blocks, default true
    cursor : true  // add fake cursor, default true
  });
};
              
            
!
999px

Console