所有分类
  • 所有分类
  • Html5资源
  • React资源
  • Vue资源
  • Php资源
  • ‌小程序资源
  • Python资源

超强js打字机效果库之Typed.js

简介

Typed.js 是一个轻量级的 JavaScript 库,用于创建打字机效果。它可以让文本以打字机的方式逐字显示,支持多种配置选项和动画效果。

主要特性

  • 简单易用: 只需几行代码即可实现打字机效果
  • 高度可配置: 支持速度控制、循环播放、暂停等功能
  • 多种动画: 支持删除、暂停、循环等动画模式
  • 无依赖: 纯 JavaScript 实现,无需其他依赖库
  • 响应式: 支持移动端和桌面端

安装

npm install typed.js
# OR
yarn install typed.js

参数

—- 参数名 —— 类型 —— 默认值 —— 描述 —
stringsArray[]要显示的字符串数组
stringsElementString、Elementnull包含字符串的 HTML 元素的 ID
typeSpeedNumber0打字速度(每秒字符数)
startDelayNumber0开始打字前的延迟时间(毫秒)
backSpeedNumber0删除字符的速度(每秒字符数)
smartBackspaceBooleanfalse是否智能删除字符
shuffleBooleanfalse是否随机打乱字符串
backDelayNumber700删除字符后的延迟时间(毫秒)
fadeOutBooleanfalse是否淡出效果
fadeOutClassString淡出效果的 CSS 类名
fadeOutDelayNumber500淡出效果的延迟时间(毫秒)
loopBooleanfalse是否循环播放
loopCountNumberInfinity循环播放次数
showCursorBooleantrue是否显示光标
cursorCharString光标字符
autoInsertCssBooleantrue是否自动插入 CSS 样式
attrString‘placeholder’要打字的属性名
bindInputFocusBooleanfalse是否绑定输入框焦点事件
contentTypeString‘html’内容类型(’html’ 或 ‘null’)

事件

事件名描述
onBegin打字机开始时触发(只触发一次)
onComplete打字机完成打字时触发
preStringTyped打字前触发
onStringTyped打字后触发
onLastStringBackspaced最后一个字符串删除时触发
onTypingPaused打字暂停时触发
onTypingResumed打字恢复时触发
onReset打字机重置时触发
onStop打字机停止时触发
onStart打字机动画开始时触发(loop: true, 会触发多次)
onDestroy打字机销毁时触发

使用示例

基础使用

import React from "react";
import Typed from "typed.js";

function TypedComponent() {
  const el = React.useRef(null);

  React.useEffect(() => {
    const typed = new Typed(el.current, {
      strings: ["Hello Typed.js", "https://code.ifrontend.net", "腾辉源码分享"],
      typeSpeed: 50,
      cursorChar: "_",
      loop: true,
      showCursor: false,
    });

    return () => {
      typed.destroy();
    };
  }, []);

  return (
    <div className="container p-10">
      <span ref={el} className="text-xl font-bold text-gray-800" />
    </div>
  );
}

export default TypedComponent;

Input 使用

import React from "react";
import Typed from "typed.js";

function MyComponent() {
  const el = React.useRef(null);

  React.useEffect(() => {
    const typed = new Typed(el.current, {
      strings: ["Hello Typed.js", "https://code.ifrontend.net", "腾辉源码分享"],
      typeSpeed: 50,
      cursorChar: "_",
      loop: true,
      showCursor: false,
    });

    return () => {
      typed.destroy();
    };
  }, []);

  return (
    <div className="container p-10">
      <input
        ref={el}
        placeholder="请输入"
        className="border-2 border-gray-300 rounded-md p-2"
      />
    </div>
  );
}

export default MyComponent;

颜色设置

React.useEffect(() => {
  const typed = new Typed(el.current, {
    strings: [
      "Hello Typed.js",
      "<span class='text-red-500'>https://code.ifrontend.net</span>",
      "<span class='text-blue-500'>腾辉源码分享</span>",
    ],
    typeSpeed: 50,
    cursorChar: "_",
    backSpeed: 0,
    loop: true,
  });

  return () => {
    typed.destroy();
  };
}, []);

资源下载
下载价格免费
注意:本网站资源属于虚拟产品,不支持退款。请谨慎购买! 购买后资源无法下载,请联系客服QQ:844475003,微信号:th844475003。
原文链接:https://code.ifrontend.net/archives/620,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?