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

vue3 css实现文字输出带光标显示,文字输出完毕,光标消失的效果

Vue实现过程如下:

<template><div ><p ref="dom_element" class="typing" :class="{over_fill: record_input_over}"></p></div>
</template>
<script setup>
import {onMounted, ref} from "vue";
const wrap_time_out = ref()
const inner_time_out = ref()
const record_input_over = ref(false)
const dom_element = ref(null)onMounted(()=>{// 也可以通过这种方式实现而不是通过ref// const typingElement = document.getElementsByClassName("typing")[0];const typingDelay = 50; // 每个字符打印的时间间隔const cursorDelay = 500; // 光标闪烁的时间间隔const text = "This is an example of typing animation.";function type_my() {// 计算出应该打印的文本const currentText  = text.slice(0, typingIndex);// 更新显示的文本和光标位置// typingElement.textContent = currentText;dom_element.value.textContent = currentText;// 如果还没打印完所有文本,则继续打印if (typingIndex < text.length) {typingIndex++;inner_time_out.value = setTimeout(type_my, typingDelay);}else {// 清除 定时器clearTimeout(wrap_time_out.value)clearTimeout(inner_time_out.value)// 当输入完成后通过控制样式将光标清除record_input_over.value = true}}let typingIndex = 0;// 等待一段时间,让用户有时间看到光标wrap_time_out.value = setTimeout(type_my, cursorDelay);
})</script><style >
html, body{height: 100%;width: 100%;//background-color: pink;
}.typing {display: inline-block;white-space: nowrap;overflow: hidden;font-size: 24px;line-height: 1.5;
}
.typing::after{content: '|';animation: blink 0.8s infinite;padding-left: 5px;
}
.over_fill::after{content: "";animation: none;padding-left: 0;
}
@keyframes blink {50% {opacity: 0;}
}
</style>

效果如下:

在这里插入图片描述
在这里插入图片描述

输入完成则光标消失,效果如下:

在这里插入图片描述

http://www.lryc.cn/news/517445.html

相关文章:

  • 什么情况会导致JVM退出?
  • CentOS7修改Docker默认存储路径
  • OpenCV相机标定与3D重建(46)将三维空间中的点投影到二维图像平面上函数projectPoints()的使用
  • 基于Elasticsearch8的向量检索实现相似图形搜索
  • springboot+vue使用easyExcel实现导出功能
  • ffmpeg-avio实战:打开本地文件或者网络直播流dome
  • css预处理器sass
  • VulnHub-Acid(1/100)
  • MATLAB语言的正则表达式
  • 通过 route 或 ip route 管理Linux主机路由
  • MYSQL--------SQL 注入简介MySQL SQL Mode 简介
  • 第6章——HTTP首部
  • 多行输入模式(dquote> 提示符)double quote(双引号)
  • 【什么是MVCC?】
  • HarmonyOS开发:粒子动画应用实战
  • 数据库课设——网上花店销售管理系统(上)
  • 用于AI的 数据存储其获取介绍
  • flutter 专题二十四 Flutter性能优化在携程酒店的实践
  • L28.【LeetCode笔记】移动零(三种解法)
  • jenkins入门10--自动化构建
  • el-table拖拽表格
  • 如何轻松反转C# List<T>中的元素顺序
  • Transformer中Self-Attention以及Multi-Head Attention模块详解(附pytorch实现)
  • 在Nvidia Jetson ADX Orin中使用TensorRT-LLM运行llama3-8b
  • 六十一:HTTP/2的问题及HTTP/3的意义
  • IOS开发如何从入门进阶到高级
  • 非一般的小数:小数的概念新解、小数分类、浮点数的存储
  • 关于游戏销量的思考
  • JuiceFS 详解:一款为云原生设计的高性能分布式文件系统
  • 百度Android面试题及参考答案 (下)