A JavaScript Typing Animation Library

Related tags

Effect typed.js
Overview

Build Status Code Climate GitHub release npm GitHub license

Live Demo | View All Demos | View Full Docs | mattboldt.com

Typed.js is a library that types. Enter in any string, and watch it type at the speed you've set, backspace what it's typed, and begin a new sentence for however many strings you've set.


Installation

Choose One

npm install typed.js
yarn add typed.js
bower install typed.js

CDN

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

Setup

This is really all you need to get going.

// Can also be included with a regular script tag
import Typed from 'typed.js';

var options = {
  strings: ['<i>First</i> sentence.', '&amp; a second sentence.'],
  typeSpeed: 40
};

var typed = new Typed('.element', options);

Use with ReactJS

Hook-based function component: https://jsfiddle.net/mattboldt/60h9an7y/

Class component: https://jsfiddle.net/mattboldt/ovat9jmp/

Use with Vue.js

Check out the Vue.js component: https://github.com/Orlandster/vue-typed-js

Use it as WebComponent

Check out the WebComponent: https://github.com/Orlandster/wc-typed-js

Wonderful sites that have used (or are using) Typed.js

https://github.com/features/package-registry

https://slack.com/

https://envato.com/

https://gorails.com/

https://productmap.co/

https://www.typed.com/

https://apeiron.io

https://git.market/

https://commando.io/

http://testdouble.com/agency.html

https://www.capitalfactory.com/

http://www.maxcdn.com/

https://www.powerauth.com/


Strings from static HTML (SEO Friendly)

Rather than using the strings array to insert strings, you can place an HTML div on the page and read from it. This allows bots and search engines, as well as users with JavaScript disabled, to see your text on the page.

<script>
  var typed = new Typed('#typed', {
    stringsElement: '#typed-strings'
  });
</script>
<div id="typed-strings">
  <p>Typed.js is a <strong>JavaScript</strong> library.</p>
  <p>It <em>types</em> out sentences.</p>
</div>
<span id="typed"></span>

Type Pausing

You can pause in the middle of a string for a given amount of time by including an escape character.

var typed = new Typed('.element', {
  // Waits 1000ms after typing "First"
  strings: ['First ^1000 sentence.', 'Second sentence.']
});

Smart Backspacing

In the following example, this would only backspace the words after "This is a"

var typed = new Typed('.element', {
  strings: ['This is a JavaScript library', 'This is an ES6 module'],
  smartBackspace: true // Default value
});

Bulk Typing

The following example would emulate how a terminal acts when typing a command and seeing its result.

var typed = new Typed('.element', {
  strings: ['git push --force ^1000\n `pushed to origin with option force`']
});

CSS

CSS animations are built upon initialization in JavaScript. But, you can customize them at your will! These classes are:

/* Cursor */
.typed-cursor {
}

/* If fade out option is set */
.typed-fade-out {
}

Customization

var typed = new Typed('.element', {
  /**
   * @property {array} strings strings to be typed
   * @property {string} stringsElement ID of element containing string children
   */
  strings: [
    'These are the default values...',
    'You know what you should do?',
    'Use your own!',
    'Have a great day!'
  ],
  stringsElement: null,

  /**
   * @property {number} typeSpeed type speed in milliseconds
   */
  typeSpeed: 0,

  /**
   * @property {number} startDelay time before typing starts in milliseconds
   */
  startDelay: 0,

  /**
   * @property {number} backSpeed backspacing speed in milliseconds
   */
  backSpeed: 0,

  /**
   * @property {boolean} smartBackspace only backspace what doesn't match the previous string
   */
  smartBackspace: true,

  /**
   * @property {boolean} shuffle shuffle the strings
   */
  shuffle: false,

  /**
   * @property {number} backDelay time before backspacing in milliseconds
   */
  backDelay: 700,

  /**
   * @property {boolean} fadeOut Fade out instead of backspace
   * @property {string} fadeOutClass css class for fade animation
   * @property {boolean} fadeOutDelay Fade out delay in milliseconds
   */
  fadeOut: false,
  fadeOutClass: 'typed-fade-out',
  fadeOutDelay: 500,

  /**
   * @property {boolean} loop loop strings
   * @property {number} loopCount amount of loops
   */
  loop: false,
  loopCount: Infinity,

  /**
   * @property {boolean} showCursor show cursor
   * @property {string} cursorChar character for cursor
   * @property {boolean} autoInsertCss insert CSS for cursor and fadeOut into HTML <head>
   */
  showCursor: true,
  cursorChar: '|',
  autoInsertCss: true,

  /**
   * @property {string} attr attribute for typing
   * Ex: input placeholder, value, or just HTML text
   */
  attr: null,

  /**
   * @property {boolean} bindInputFocusEvents bind to focus and blur if el is text input
   */
  bindInputFocusEvents: false,

  /**
   * @property {string} contentType 'html' or 'null' for plaintext
   */
  contentType: 'html',

  /**
   * Before it begins typing
   * @param {Typed} self
   */
  onBegin: (self) => {},

  /**
   * All typing is complete
   * @param {Typed} self
   */
  onComplete: (self) => {},

  /**
   * Before each string is typed
   * @param {number} arrayPos
   * @param {Typed} self
   */
  preStringTyped: (arrayPos, self) => {},

  /**
   * After each string is typed
   * @param {number} arrayPos
   * @param {Typed} self
   */
  onStringTyped: (arrayPos, self) => {},

  /**
   * During looping, after last string is typed
   * @param {Typed} self
   */
  onLastStringBackspaced: (self) => {},

  /**
   * Typing has been stopped
   * @param {number} arrayPos
   * @param {Typed} self
   */
  onTypingPaused: (arrayPos, self) => {},

  /**
   * Typing has been started after being stopped
   * @param {number} arrayPos
   * @param {Typed} self
   */
  onTypingResumed: (arrayPos, self) => {},

  /**
   * After reset
   * @param {Typed} self
   */
  onReset: (self) => {},

  /**
   * After stop
   * @param {number} arrayPos
   * @param {Typed} self
   */
  onStop: (arrayPos, self) => {},

  /**
   * After start
   * @param {number} arrayPos
   * @param {Typed} self
   */
  onStart: (arrayPos, self) => {},

  /**
   * After destroy
   * @param {Typed} self
   */
  onDestroy: (self) => {}
});

Contributing

View Contribution Guidelines

end

Thanks for checking this out. If you have any questions, I'll be on Twitter.

If you're using this, let me know! I'd love to see it.

It would also be great if you mentioned me or my website somewhere. www.mattboldt.com

Comments
  • Cannot get it to work!

    Cannot get it to work!

    Description

    Hey Guys, hoping someone can help me with this problem.

    I feel ridiculous, but I can't get the simple example from "Strings from static HTML (SEO Friendly)" section in the README to work.

    Steps to Reproduce

    My code is as follows:

    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="js/typed.min.js"></script>
    
      <script>
        var typed = new Typed('.element', {
          stringsElement: '#typed-strings'
        });
      </script>
    </head>
    
    <body>
      <div id="typed-strings">
        <p>Typed.js is an <strong>Awesome</strong> library.</p>
        <p>It <em>types</em> out sentences.</p>
      </div>
      <span id="typed"></span>
    
    </body>
    </html>
    

    Expected behavior: I've tried running this in both chrome and safari but to no avail.

    Actual behavior: The text is just presented statically with no typewriter effect. I'm getting the error:

    Uncaught TypeError: Cannot read property 'tagName' of null
        at t.value (typed.min.js:10)
        at new t (typed.min.js:10)
        at test.html:5
    

    NB: typed.min.js is the latest version (2.0.2) downloaded directly from github.

    opened by shaq 20
  • Blinking cursor is on a new line when the typed element is `p` (paragraph)

    Blinking cursor is on a new line when the typed element is `p` (paragraph)

    If I'm initializing the plugin on a

    element, the blinking cursor is always on a line below the text

    <p id="a"></p> $("#a").typed({ strings : ["test"] });

    opened by daattali 18
  • Uncaught TypeError: Cannot read property 'tagName' of null

    Uncaught TypeError: Cannot read property 'tagName' of null

    Description

    I have loaded the JS from your RAWGIT file @mattboldt in the head of my WordPress header.php

    <script src="https://cdn.rawgit.com/mattboldt/typed.js/master/lib/typed.min.js"></script>

    I have tried multiple ways next to implement the Text typing but this is the last one I tried.

    [div id="typed-strings"] [p]Typed.js is an Awesome library.[/p] [p]It types out sentences.[/p] [/div] [span id="typed"][/span]

    I have loaded the JS in the footer and the header to see where I might be going wrong but I keep getting this error in my Console

    typed.min.js:10 Uncaught TypeError: Cannot read property 'tagName' of null at t.value (typed.min.js:10) at new t (typed.min.js:10) at sitename.isamazing.co.za/:299 at sitename.isamazing.co.za/:302

    Many thanks

    opened by sixfootjames 17
  • How to reinitiate the plugin?

    How to reinitiate the plugin?

    Hi there, I am using the plugin in multiple sections of my page,and each section has a different text and it works fine on load.Now user can see each section at a time in the page and on scroll next or previous section is shown to user. and I have to show typing effect when each section is shown but I am not able to reinitiate it. Can you please help me out for how to reinitiate the plugin ?

    Cheers Nisar

    opened by nisar5071 15
  • IE issues

    IE issues

    In IE 10, console error:

    SCRIPT5042: Variable undefined in strict mode typed.js, line 8 character 3

    Removing "use strict"; causes another error...

    SCRIPT65535: Argument not optional typed.js, line 87 character 8

    Any ideas?

    opened by mbarwick83 11
  • Uncaught TypeError: $(...).typed is not a function

    Uncaught TypeError: $(...).typed is not a function

    Description

    Working fine with https://cdnjs.cloudflare.com/ajax/libs/typed.js/1.1.7/typed.min.js Fiddle

    Not working with (any release from 2.0.0) https://cdnjs.cloudflare.com/ajax/libs/typed.js/2.0.9/typed.min.js Fiddle

    Error: Uncaught TypeError: $(...).typed is not a function at typeWords

    opened by pramitbiswas 10
  • Callback clears timeouts -- why?

    Callback clears timeouts -- why?

    This took me forever to debug (working around #12)

          $(".type-code").typed
            callback: ->
              setTimeout ->
                console.log("why u no fire?")
              , 1000
    

    This never fires because Typed clears the callback (because my callback happens to return the callback ID). Explicitly returning true afterward resolves the issue, but that was really difficult to figure out.

    opened by searls 10
  • Is it possible (or viable to implement) to treat a group of characters as just one character?

    Is it possible (or viable to implement) to treat a group of characters as just one character?

    Description

    When working with emojis, you can see the multiple characters it is composed by, and it looks a bit weird, and some of the characters only render as the typical "character not found symbol".

    Demo

    Demo with emojis

    Steps to Reproduce

    1. Add typed.js library
    2. Configure strings to contain emojis
    3. Watch with horror what happens xD.

    Expected behavior: I expect to see only the final emoji rendered on the screen.

    Actual behavior: Each of the characters that compose the emoji are shown and finally you can see the result of the composition of the multiple characters into the final emoji, with some "character not found symbol" in between.

    Reproduces how often: 100%

    Additional Information

    I understand that this is not exactly a problem with the library itself, but I'm wondering if there is some way to indicate that the emoji is a group of characters that should be treated as only one when typing your strings (similar as when you add a pause "^1000" of one second for example) or if it would be viable (and acceptable as a feature for the library) to implement.

    Feature 
    opened by crammag 9
  • Uncaught TypeError: Cannot read property 'substr' of undefined

    Uncaught TypeError: Cannot read property 'substr' of undefined

    Description

    When setting the strings property of a Typed instance to an array with length greater than 0 and resetting the instance with Typed.reset(); the following error occures:this.typed_instance.strings = new_text; this.typed_instance.reset();

    Uncaught TypeError: Cannot read property 'substr' of undefined
        at eval (typed.js?d97d:230)
    

    If you instead do:

    var textArray = ["string", "string"];
    typed.destroy();
    typed = new Typed('#control_panel_text span.paragraph', { strings: textArray });
    

    it works fine.

    Steps to Reproduce

    var textArray = ["string", "string"]; typed.strings = textArray; typed.reset(); // also tried passing true as argument

    Expected behavior: resets the string array with a new one and properly progresses through a multimember array without throwing the error using the reset() method.

    Notes

    Im just trying to update the same instance with new text arrays. Maybe the reset method wasnt meant for that and destroying the instance is correct.

    Wontfix Bug: minor 
    opened by HauntedSmores 9
  •  t.el is null

    t.el is null

    Description

    I'm using Typed.JS on rawgit. I've tried direct sourcing your code using