typed.js README

The project's README from github.com/mattboldt/typed.js. As one markdown file: README.md.

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.


Licensing

GPL-3.0 License Limited Commercial License Unlimited Commercial License

Free for use on personal and open source projects

View License

For use on a single commercial project

Purchase License

View License

For use on unlimited commercial projects

Purchase License

View License

Installation

For use with a build tool like Vite, and/or in a React application, install with NPM or Yarn.

NPM

npm install typed.js

Yarn

yarn add typed.js

General ESM Usage

import Typed from 'typed.js';

const typed = new Typed('#element', {
  strings: ['<i>First</i> sentence.', '&amp; a second sentence.'],
  typeSpeed: 50,
});

ReactJS Usage

import React, { useEffect, useRef } from 'react';
import Typed from 'typed.js';

function MyComponent() {
  // Create reference to store the DOM element containing the animation
  const el = useRef(null);

  useEffect(() => {
    const typed = new Typed(el.current, {
      strings: ['<i>First</i> sentence.', '&amp; a second sentence.'],
      typeSpeed: 50,
    });

    return () => {
      // Destroy Typed instance during cleanup to stop animation
      typed.destroy();
    };
  }, []);

  return (
    <div className="App">
      <span ref={el} />
    </div>
  );
}

CDN

<script src="https://unpkg.com/[email protected]/dist/typed.umd.js"></script>

For use directly in the browser via <script> tag:

  <!-- Element to contain animated typing -->
  <span id="element"></span>

  <!-- Load library from the CDN -->
  <script src="https://unpkg.com/[email protected]/dist/typed.umd.js"></script>

  <!-- Setup and start animation! -->
  <script>
    var typed = new Typed('#element', {
      strings: ['<i>First</i> sentence.', '&amp; a second sentence.'],
      typeSpeed: 50,
    });
  </script>
</body>

More complex 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://forwardemail.net

https://codesignal.com

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

typed.js (mattboldt/typed.js)

Searching Database…

16.3K ·GitHub
这是什么?Typed.js 是一个 JavaScript 库,用于制作打字动画:它会按你设定的速度逐字输入字符串,然后退格删除,再循环切换到下一个字符串。
这是完整的动画框架,还是只是我可以直接引入的小脚本?它是一个轻量、功能单一的库,而不是框架。你需要用 'new Typed()' 创建一个实例,指定目标元素,并传入包含字符串和选项的配置对象。
我该如何把它加入项目?可以通过 NPM 或 Yarn 安装,以便配合构建工具或 React 使用;也可以将其作为 ESM 模块导入;或者直接通过 CDN 的 script 标签在浏览器中引入。
使用它需要构建工具或框架吗?不需要。它没有运行时依赖,可以通过 CDN 独立使用;如果你已经在用打包工具或 React,也可以用 ESM 导入。
打字选项有什么需要注意的坑吗?smartBackspace 只会删除字符串中与上一个字符串不同的部分,因此如果你的字符串彼此重叠很多,实际被删除的内容就会发生变化。该选项默认开启。
这个能在哪里运行?它可以在任何网页浏览器中运行,通过 ID 或选择器指定目标 DOM 元素。
我可以自定义光标,或者让文字淡出而不是退格删除吗?可以。'showCursor' 和 'cursorChar' 用于控制闪烁的光标;'fadeOut'(配合 'fadeOutClass' 和 'fadeOutDelay')则会用 CSS 淡出动画取代退格删除。
我能控制单个字符串内部的时间,比如在句子中间暂停吗?可以。你可以在字符串中插入停顿标记,例如 '^1000',表示等待 1000 毫秒后再继续输入,例如 'First ^1000 sentence.'
它支持打字事件的回调函数吗?支持。'onBegin'、'onComplete'、'onStringTyped'、'onTypingPaused'、'onStart' 和 'onDestroy' 等选项可以让你在打字的每个阶段挂接自定义逻辑。
字符串可以按随机顺序输入吗?可以。设置 'shuffle: true' 后,会打乱所提供字符串的输入顺序,而不是按顺序依次输入。
Creator
mattboldt
Size
5.8 MB
Stars
16.3K
Age
13y
Commits · 90 Days