Skip to content

TypewriterText 打字机文本 ​

新粗野主义风格的打字机效果文本组件,逐字符显示文本并带有闪烁光标动画。适用于引导页、欢迎语、代码展示等场景。

预览 ​

Preview

基础用法

循环播放

尺寸与粗细

无光标

安装 ​

pnpm dlx brutx-vue@latest add typewriter-text

用法 ​

vue
<script setup>
import { TypewriterText } from 'brutx-ui-vue'
</script>

<template>
    <TypewriterText
        text="欢迎使用 BrutxUI!"
        :speed="80"
        cursor
    />
</template>

循环播放 ​

vue
<template>
    <TypewriterText
        text="持续打字效果..."
        :speed="60"
        :delay="2000"
        loop
        cursor
    />
</template>

事件监听 ​

vue
<script setup>
import { TypewriterText } from 'brutx-ui-vue'

function onStart() {
    console.log('开始打字')
}

function onComplete() {
    console.log('打字完成')
}
</script>

<template>
    <TypewriterText
        text="打字完成后触发回调"
        :speed="80"
        @start="onStart"
        @complete="onComplete"
    />
</template>

变体 ​

变体CSS 类
normalfont-normal
mediumfont-medium
boldfont-bold
blackfont-black

尺寸 ​

尺寸CSS 类光标高度
smtext-smh-3
defaulttext-baseh-4
lgtext-lgh-5
xltext-xlh-6
2xltext-2xlh-7
vue
<template>
    <TypewriterText
        text="大号粗体文本"
        size="xl"
        weight="bold"
        :speed="100"
    />
</template>

API 参考 ​

TypewriterText — 属性
名称类型默认值说明
text
必填
string
—
逐字显示的完整文本内容。
详情

声明类型

string

默认值详情

未声明默认值
—

来源

TypewriterText.vue:12

packages/ui/src/components/typewriter-text/TypewriterText.vue

speed
number | undefined
50
每显示一个字符之间的间隔时间,单位为毫秒。
详情

声明类型

number | undefined

默认值详情

解析值
50

来源

TypewriterText.vue:14

packages/ui/src/components/typewriter-text/TypewriterText.vue

delay
number | undefined
0
首次开始打字前等待的时间;循环模式下也作为每轮重新开始前的间隔。
详情

声明类型

number | undefined

默认值详情

解析值
0

来源

TypewriterText.vue:16

packages/ui/src/components/typewriter-text/TypewriterText.vue

loop
boolean | undefined
false
在文本显示完成后持续循环播放。
详情

声明类型

boolean | undefined

字面量

  • false
  • true

默认值详情

解析值
false

来源

TypewriterText.vue:18

packages/ui/src/components/typewriter-text/TypewriterText.vue

cursor
boolean | undefined
true
显示打字光标;非循环模式下文本完成后光标隐藏。
详情

声明类型

boolean | undefined

字面量

  • false
  • true

默认值详情

解析值
true

来源

TypewriterText.vue:20

packages/ui/src/components/typewriter-text/TypewriterText.vue

size
"2xl" | "default" | "lg" | "sm" | "xl" | undefined
"default"
设置打字文本的字号预设。
详情

声明类型

NonNullable<"default" | "lg" | "sm" | "xl" | "2xl" | null | undefined> | undefined

字面量

  • "2xl"
  • "default"
  • "lg"
  • "sm"
  • "xl"

默认值详情

解析值
"default"

来源

TypewriterText.vue:22

packages/ui/src/components/typewriter-text/TypewriterText.vue

weight
"black" | "bold" | "medium" | "normal" | undefined
"normal"
设置打字文本的字重。
详情

声明类型

NonNullable<"medium" | "bold" | "normal" | "black" | null | undefined> | undefined

字面量

  • "black"
  • "bold"
  • "medium"
  • "normal"

默认值详情

解析值
"normal"

来源

TypewriterText.vue:24

packages/ui/src/components/typewriter-text/TypewriterText.vue

class
string | undefined
undefined
追加到打字文本根元素的自定义 CSS 类。
详情

声明类型

string | undefined

默认值详情

解析值
undefined

来源

TypewriterText.vue:26

packages/ui/src/components/typewriter-text/TypewriterText.vue

TypewriterText — 事件
名称签名或类型说明
@complete
[]
一轮文本显示完成时发出;循环模式下每轮完成都会触发。
详情

声明类型

[]

来源

TypewriterText.vue

packages/ui/src/components/typewriter-text/TypewriterText.vue

@start
[]
每轮打字动画开始时发出。
详情

声明类型

[]

来源

TypewriterText.vue

packages/ui/src/components/typewriter-text/TypewriterText.vue

可访问性 ​

  • ARIA 属性:组件使用 aria-live="polite" 确保屏幕阅读器能感知文本变化;光标使用 aria-hidden="true" 标记为装饰性元素
  • 动效降级:当用户偏好 prefers-reduced-motion: reduce 时,直接显示完整文本,跳过动画;该偏好实时响应系统设置变化

常见问题 ​

Q: text 属性变化时会怎样?

A: text 属性变化时会重新开始打字动画。

Q: 组件卸载时定时器如何处理?

A: 组件卸载时会自动清理所有定时器(包括打字定时器和延迟启动定时器),避免内存泄漏。

Q: 循环模式下的行为是什么?

A: 循环模式下,打字完成后会等待 delay 毫秒后重新开始。

Q: 如何自定义样式?

A: 组件根元素始终带有 brutx-typewriter CSS 类,可用于自定义样式覆盖。

蛮力铸就。