打字机文字效果(vue2版本)

打字机文字效果

示例图片

在这里插入图片描述

示例组件代码

<template>
  <div id="text-typing">
    <pre class="textClass">{{ displayText }}<span class="cursor">|</span></pre>
  </div>
</template>

<script>
export default {
  props: {
    lines: {
      type: Array,
      default() {
        return ['希望广大老年朋友保持老骥伏枥、老当益壮的健康心态和进取精神,既要老有所养、老有所乐,又要老有所为,为推进中国式现代化贡献‘银发力量’。','近日给“银龄行动”老年志愿者代表回信,并祝全国老年朋友们健康长寿、生活幸福。'];
      },
    },
    timeNum: {
      type: Number,
      default: 100, // 默认打字速度为100ms
    },
  },
  data() {
    return {
      displayText: '',
      currentLineIndex: 0,
      currentCharIndex: 0,
      typingInterval: null,
    };
  },
  methods: {
    typeLine() {
      if (this.currentCharIndex < this.lines[this.currentLineIndex].length) {
        // 逐字显示当前行的文本
        this.displayText +=
          this.lines[this.currentLineIndex][this.currentCharIndex];
        this.currentCharIndex++;
      } else {
        // 当前行显示完毕,准备显示下一行
        if (this.currentLineIndex + 1 < this.lines.length) {
          this.displayText += '\n'; // 添加换行符
        }
        this.currentLineIndex++;
        this.currentCharIndex = 0;
        if (this.currentLineIndex >= this.lines.length) {
          // 所有行显示完毕,重置并重新开始
          this.currentLineIndex = 0;
          clearInterval(this.typingInterval); // 清除定时器
          // setTimeout(this.startTyping, 5000); // 等待5秒后重新开始
        }
      }
    },
    startTyping() {
      // 清空显示文本,重新开始
      this.displayText = '';
      if (this.typingInterval) {
        clearInterval(this.typingInterval);
      }
      this.typingInterval = setInterval(this.typeLine, this.timeNum); //这里调整打字速度
    },
  },
  mounted() {
    if (this.lines.length > 0) {
      this.startTyping();
    }
  },
  beforeDestroy() {
    clearInterval(this.typingInterval); // 组件销毁前清除定时器
  },
};
</script>

<style>
.cursor {
  animation: blink 1s infinite;
}

@keyframes blink {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0;
  }
}
.textClass {
  font-family: Arial, sans-serif;
  font-size: 16px;
  line-height: 30px;
  color: #333;
  white-space: break-spaces;
  word-wrap: break-word;
  text-align: left;
}
</style>

### 使用 Typed.js 和 TypeIt 实现在 Vue.js 中的打字机效果 #### 使用 Typed.js 创建打字动画效果 为了在 Vue.js 项目中使用 Typed.js 来创建打字动画效果,首先需要安装该库。可以通过 npm 或 yarn 安装: ```bash npm install typed.js --save ``` 或者 ```bash yarn add typed.js ``` 接着,在组件内部引入并初始化 Typed.js: ```javascript <template> <div id="typed-text"></div> </template> <script> import { onMounted } from 'vue'; import Typed from 'typed.js'; export default { setup() { let typed; onMounted(() => { const options = { strings: ["First sentence.", "Second sentence."], typeSpeed: 40, backSpeed: 40, loop: true }; typed = new Typed('#typed-text', options); }); return {}; } }; </script> ``` 此代码片段展示了如何利用 `onMounted` 生命周期钩子函数来确保 DOM 已经加载完毕后再启动 Typed.js 动画[^1]。 #### 利用 TypeIt 插件实现更简洁的方式 对于寻求更加简便方法的人来说,TypeIt 提供了一种更为直观的方式来实现在 Vue.js 上的文字打字机效果。同样先要完成依赖项的安装工作: ```bash npm install @typeit/vue-next@latest --save ``` 随后按照如下方式定义组件结构与逻辑部分: ```html <template> <span v-type.it="{ speed: 50, cursor: false }"> Hello world! This is a message that will be typed out. </span> </template> <script> import { defineComponent } from 'vue'; import VTypeIt from '@typeit/vue-next'; export default defineComponent({ components: { VTypeIt }, }); </script> ``` 上述实例说明了怎样借助于自定义指令 `v-type.it` 轻松地将 TypeIt 集成到 Vue 组件之中,从而简化了配置过程并提高了可读性[^2]。 #### 结合 Spring Boot 和 Vue 构建复杂应用案例 如果目标是构建一个具有前后端分离架构的应用程序,并且想要加入类似 ChatGPT 的交互体验,则可以考虑参考基于 Spring Boot 和 Vue 技术栈所搭建的例子。这类解决方案不仅能够提供良好的视觉反馈给用户,还支持实时数据交换等功能特性[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值