当前位置:首页 > VUE

vue实现打字机

2026-01-12 01:13:30VUE

实现打字机效果的基本思路

在Vue中实现打字机效果,可以通过动态更新显示文本内容,模拟逐字打印的效果。核心是利用定时器逐步追加字符到显示字符串中。

基础实现方法

  1. 定义数据 在Vue组件的data中定义需要显示的完整文本和当前显示的文本:

    data() {
      return {
        fullText: '这里是需要逐字显示的文字内容',
        displayedText: '',
        typingSpeed: 100 // 打字速度(毫秒/字符)
      }
    }
  2. 创建打字函数 使用递归或循环方式逐步追加字符:

    methods: {
      typeWriter() {
        if (this.displayedText.length < this.fullText.length) {
          this.displayedText = this.fullText.substring(0, this.displayedText.length + 1)
          setTimeout(this.typeWriter, this.typingSpeed)
        }
      }
    }
  3. 调用函数 在mounted生命周期钩子中启动打字效果:

    mounted() {
      this.typeWriter()
    }

进阶实现方案

  1. 添加光标闪烁效果 在模板中添加光标元素并设置CSS动画:

    <div class="typewriter">
      {{ displayedText }}<span class="cursor">|</span>
    </div>

    CSS样式:

    .cursor {
      animation: blink 1s infinite;
    }
    @keyframes blink {
      0%, 100% { opacity: 1; }
      50% { opacity: 0; }
    }
  2. 支持多行文本 将fullText改为数组,循环打字:

    data() {
      return {
        lines: ['第一行文字', '第二行文字', '第三行文字'],
        currentLine: 0,
        displayedText: ''
      }
    }

    更新打字函数:

    typeWriter() {
      if (this.currentLine < this.lines.length) {
        const currentLineText = this.lines[this.currentLine]
        if (this.displayedText.length < currentLineText.length) {
          this.displayedText = currentLineText.substring(0, this.displayedText.length + 1)
          setTimeout(this.typeWriter, this.typingSpeed)
        } else {
          setTimeout(() => {
            this.currentLine++
            this.displayedText = ''
            this.typeWriter()
          }, 1000) // 行间延迟
        }
      }
    }

使用第三方库

  1. vue-typed-js 安装库:

    npm install vue-typed-js

    使用示例:

    import Vue from 'vue'
    import VueTypedJs from 'vue-typed-js'
    
    Vue.use(VueTypedJs)

    模板中使用:

    <vue-typed-js :strings="['第一段文字', '第二段文字']" :loop="true">
      <span class="typing"></span>
    </vue-typed-js>
  2. 自定义指令实现 创建全局指令:

    Vue.directive('typewriter', {
      inserted(el, binding) {
        const text = binding.value
        let i = 0
        const speed = binding.arg || 100
    
        function type() {
          if (i < text.length) {
            el.innerHTML += text.charAt(i)
            i++
            setTimeout(type, speed)
          }
        }
    
        type()
      }
    })

    使用指令:

    <div v-typewriter="'这里是指令实现的打字效果'" v-typewriter:50></div>

注意事项

  1. 性能优化 长时间打字效果可能消耗资源,使用前清除已有定时器:

    beforeDestroy() {
      clearTimeout(this.typeTimer)
    }
  2. 响应式设计 当文本内容变化时重置效果:

    watch: {
      fullText() {
        this.displayedText = ''
        this.typeWriter()
      }
    }
  3. 可访问性考虑 为屏幕阅读器添加aria属性:

    vue实现打字机

    <div aria-live="polite">{{ displayedText }}</div>

标签: 打字机vue
分享给朋友:

相关文章

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <ul…

vue底层实现

vue底层实现

Vue.js 的底层实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现细节: 响应式系统 Vue 2…