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

建设部继续教育网站建设多语言网站

建设部继续教育网站,建设多语言网站,迅雷资源做下载网站,宁波那家公司做网站好在现代前端开发中,添加一些视觉效果可以提升用户体验。其中,打字机效果是一种常见且吸引人的效果,可以用于展示动态文本。本文将介绍如何在 Vue 3 中实现打字机效果。 实现步骤 1. 创建自定义指令 我们首先创建一个自定义指令 v-typewriter…

在现代前端开发中,添加一些视觉效果可以提升用户体验。其中,打字机效果是一种常见且吸引人的效果,可以用于展示动态文本。本文将介绍如何在 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.yayakq.cn/news/579739/

相关文章:

  • 创研科技做网站怎么样教育类网站策划书
  • php网站源码删除网站开发工程师怎么考
  • PHP网站开发简单实例最近最火的关键词
  • 预付网站建设费用会计分录聊城做企业网站的
  • 专业点网站制作公司国内响应式网站案例
  • 哪个网站做外贸年费比较便宜东台建设企业网站
  • 做的比较好的网站推荐常德网站制作公司
  • 网站交互功能造价统计报表在哪个网站上做
  • 在招聘网站做电话销售怎么样怎样注册自己网站
  • 我的世界做视频封面的网站专业的企业网站开发公司
  • 泰安营销网站建设公司wordpress调用python脚本
  • 网站开发职位要求广告设计与制作专业专升本
  • 网站制作与管理技术...高端建设网站公司
  • 做网站项目后台的网站建站费用多少钱
  • 网站建设需要注意什么哪些保险哪家好
  • 可以网上做单的网站网站翻页代码
  • 如何制作外贸网站 wordpress深圳东门老街附近景点
  • 南昌建网站那家好轻极免费版wordpress主题
  • 计算机应用技术(网站开发)做专业搜索引擎优化
  • 全国建设工程执业资格注册中心网站天元建设集团有限公司衡阳分公司
  • 杭州模板建站wordpress开源吗
  • 提升网站建设中国禹路由网站建设中
  • 赣州做网站的公司有哪家好口碑好网站制作公司哪家好
  • 网站服务器搬家百度代理服务器
  • 网站服务器内部错误是怎么回事做汽配网站
  • 怎么推广自己的网站链接网站怎么做才能赚钱
  • 给公司做网站多钱甘肃省建设厅官方网站造价总站
  • 网站制作费用价格表商城微发布官网
  • 南通网站优化公司滴滴出行app开发公司
  • 创建网站收费wordpress把站