当前位置: 首页 > news >正文

使用 Vue 3 实现打字机效果

在现代前端开发中,添加一些视觉效果可以提升用户体验。其中,打字机效果是一种常见且吸引人的效果,可以用于展示动态文本。本文将介绍如何在 Vue 3 中实现打字机效果。

实现步骤

1. 创建自定义指令

我们首先创建一个自定义指令 v-typewriter,用于实现打字机效果。这个指令将逐字显示绑定的文本内容。

const typeWriter = ref(null);const typewriterDirective = (el, binding) => {const indexValue = el.getAttribute('data-index');const delay = 150; // 设置延迟时间,默认150mslet i = 0;typeWriter.value = setInterval(() => {if (binding?.value && i < binding.value.length) {if (textList.value && textList.value[indexValue]) {textList.value[indexValue].typewriterText += binding.value.charAt(i) || '';}i++;} else {clearInterval(typeWriter.value);stop(textList.value[indexValue], indexValue, true);}}, delay);
};const vTypewriter = {mounted(el, binding) {typewriterDirective(el, binding);}
};

2. 使用自定义指令

在 Vue 组件中使用自定义指令 v-typewriter。该指令会在元素挂载时自动触发,逐字显示文本内容。

<template><div class="left-content mr-16"><el-scrollbar ref="scrollRef" height="100%" class="scroll"><div class="flex mb-48" v-for="(item, index) in textList" :key="item.updateKey"><div class="user-avatar"><img v-if="item.resultTts || item.library" src="/img/avatar.png" alt="" /><img v-else src="/img/user_avatar.png" alt="" /></div><div class="ml-12"><div class="time mb-11"><span v-if="item.resultTts || item.library">智能馆员{{ item.time }}</span><span v-else>读者{{ item.time }}</span></div><div><divclass="answer":class="item.resultTts || item.library ? 'libarary-bg' : 'user-color '"><div v-if="item.resultTts || item.library"><van-loading v-if="!item.resultMessage" type="spinner" color="#1989fa" /><div v-if="item.isStop && item.stopText">{{ item.stopText }}</div><divv-if="item.resultTts && !item.isStop"v-typewriter="item.resultTts":data-index="index">{{ item.typewriterText }}</div></div><div v-else>{{ item.resultMessage }}</div></div><divv-if="(!item.isStop && item.resultTts) || !item.resultMessage"class="stop-icon mt-18"@click="stop(item, index)">停止生成</div></div><BookListv-if="item.dataList?.length && item.isStop":data-list="item.dataList"></BookList></div></div></el-scrollbar></div>
</template><script>
import { ref } from 'vue';
import { useEventBus } from '@/hooks/useEventBus';const emits = defineEmits(['watchTypeWriter', 'handleStop']);const textList = ref([]);
useEventBus('clearChatInfo', () => {textList.value = [];
});
useEventBus('changeAction', (message) => {
// message其它组件传递的数据 clearInterval(typeWriter.value);if (!textList.value.length || !message.resultTts) {textList.value.push({time: ` ${dayjs().format('HH:mm:ss')}`,isStop: false,  // 是否停止stopText: '',   // 打字机停止后的内容typewriterText: '',  // 动态展示打字机内容的文本updateKey: dayjs().valueOf(), // 每次增加一条数据的唯一key...message});return;}if (textList.value.length && message.resultTts) {textList.value[textList.value.length - 1] = {...textList.value[textList.value.length - 1],...message};}
});const stop = (item, index, isFinish = false) => {if (!item.resultMessage) {item.isStop = true;emits('handleStop');return;}if (item.isStop) {return;}const curText = document.querySelector(`[data-index="${index}"]`);item.isStop = true;item.stopText = curText?.innerText;
};
</script><style scoped lang="scss">
/* 自定义样式*/
</style>
http://www.lryc.cn/news/397333.html

相关文章:

  • unordered_map和set
  • java:运用字节缓冲输入流将文件中的数据写到集合中
  • 【机器学习】支持向量机与主成分分析在机器学习中的应用
  • SpringBoot项目架构实战之“网关zuul搭建“
  • 发挥储能系统领域优势,海博思创坚定不移推动能源消费革命
  • matlab R2016b安装cplex12.6,测试时cplex出现出现内部错误的解决方法
  • C#中的Dictionary
  • VSCode中多行文本的快速前后缩进
  • C# 8.0 新语法的学习和使用
  • 数据结构——约瑟夫环C语言链表实现
  • 【MyBatis】——入门基础知识必会内容
  • react父调用子的方法,子调用父的方法
  • C#知识|账号管理系统:UI层-添加账号窗体设计思路及流程。
  • 【机器学习】初学者经典案例(随记)
  • 进阶版智能家居系统Demo[C#]:整合AI和自动化
  • IC后端设计中的shrink系数设置方法
  • 在NVIDIA Jetson平台离线部署大模型
  • 51单片机嵌入式开发:8、 STC89C52RC 操作LCD1602原理
  • 数字化时代的供应链管理综合解决方案
  • CentOS 安装 annie/lux,以及 annie/lux 的使用
  • 拥抱UniHttp,规范Http接口对接之旅
  • Python 给存入 Redis 的键值对设置过期时间
  • 在linux中安装docker
  • 【JVM-04】线上CPU100%
  • try catch 解决大问题
  • 手动解析Collection
  • list模拟实现【C++】
  • nginx正向代理、反向代理、负载均衡
  • matlab 有倾斜的椭圆函数图像绘制
  • PTK是如何加密WLAN单播数据帧的?