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

多行文本擦除效果

实现是一个创意的文字擦除动画效果

多行文字擦除效果

实现原理

  1. 双层文本结构

    • 底层显示完整文本(白色)
    • 上层覆盖一个相同的文本,通过特殊效果实现"擦除"
  2. CSS动画技术

    • 使用CSS自定义属性(变量)配合@keyframes动画
    • 通过@property定义–p变量,使其可以在动画中变化
    • 利用linear-gradient创建渐变遮罩效果
  3. 渐变遮罩效果

    • 将上层文本设置为透明
    • 使用线性渐变背景,从透明到黑色
    • 通过改变渐变位置实现擦除效果

技术要点

  1. 布局处理

    • 使用绝对定位让擦除层完全覆盖文本
    • 设置inset: 0确保覆盖整个父容器
  2. 动画实现

    • 使用CSS动画改变–p变量值从0%到100%
    • 渐变效果:background: linear-gradient(to right, transparent var(--p), black calc(var(--p) + 30px))
    • 30px的渐变宽度提供平滑的擦除效果
  3. JavaScript处理

    • 动态获取文本内容
    • 将相同内容复制到擦除层

创新点

  1. 解决了CSS变量不能直接用于@keyframes的问题,通过@property定义CSS变量
  2. 使用渐变而非纯色实现更自然的擦除效果
  3. 双层文本结构确保文本始终可见,同时实现擦除动画

效果特点

  • 黑色背景上的白色文字
  • 从左到右的擦除动画
  • 5秒的动画时长
  • 线性动画效果
  • 动画结束后保持最终状态

这个实现巧妙地结合了CSS自定义属性、渐变和动画,创造出一个简单而优雅的文字擦除效果。

代码实现

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title><style>* {margin: 0;padding: 0;}html,body {background-color: #000;height: 100%;}.container {width: 80%;margin: 1em auto;line-height: 2;text-indent: 2em;position: relative;font-size: 20px;}.text {color: white;}.eraser {position: absolute;/* 完全覆盖父元素内容 */inset: 0;}/* 变量不可使用@keyframe动画,使用自定义属性解决 */@property --p {syntax: '<percentage>';inherits: false;initial-value: 0%;}.eraser-text {--p: 0%;color: transparent;background: linear-gradient(to right, transparent var(--p), black calc(var(--p) + 30px));animation: eraser 5s linear forwards;}@keyframes eraser {to {--p: 100%;}}</style></head><body><div class="container"><p class="text"></p><p class="eraser"><span class="eraser-text"></span></p></div><script>const text = document.querySelector('.text');const eraserText = document.querySelector('.eraser-text');text.innerHTML = `When using the canvas element or the Canvas API, rendering,animation, and user interaction usually happen on the main execution thread of a web application.The computation relating to canvas animations and rendering can have a significant impact on application performance.
The OffscreenCanvas interface provides a canvas that can be rendered off screen, decoupling the DOM and the
Canvas API so that the canvas element is no longer entirely dependent on the DOM. Rendering operations canalso be run inside a worker context, allowing you to run some tasks in a separate thread and avoid heavy work on the main thread.
OffscreenCanvas is a transferable object.OffscreenCanvas.getContext()
Returns a drawing context for the offscreen canvas, or null if the context identifier
is not supported, or the offscreen canvas has already been set to a different context mode.
OffscreenCanvas.convertToBlob()
Creates a Blob object representing the image contained
in the canvas.OffscreenCanvas.transferToImageBitmap()Creates an ImageBitmap object from the most recently rendered image of the OffscreenCanvas.See its reference for important notes on managing this ImageBitmap.`;eraserText.innerHTML = text.textContent;</script></body>
</html>

参考文献

  • 多行文本擦除效果
http://www.dtcms.com/a/561145.html

相关文章:

  • 做产品表情的网站深圳注册公司在哪里注册
  • 免费企业建站源代码深圳住房网站app
  • 数独系列算法
  • 梅州南站电商网站建设期末考试
  • 构建一个更稳定、更聪明的 PDF 翻译 Agent:从踩坑到总结
  • 【仿RabbitMQ的发布订阅式消息队列】--- 客户端模块
  • python 初学 3 --字符串编码
  • 企网站建设比价网站怎么做的
  • Linux磁盘性能优化:文件系统选择与挂载参数调整(附案例)
  • 如何建设网站首页网站备案照
  • “RAG简单介绍
  • Spring_cloud(1)
  • 终结Linux系统崩溃——Aptitude:以搜狗输入法与fcitx/ibus依赖冲突的终极解决方案为例
  • 关于 ComfyUI 的 Windows 本地部署系统环境教程(详细讲解Windows 10/11、NVIDIA GPU、Python、PyTorch环境等)
  • 网站开发包含什么百度手机
  • 部门网站建设管理典型经验材料广东住房和城乡建设厅官方网站
  • PHP 基金会宣布:Streams 现代化 将引入事件循环与异步新能力
  • 网站建设武清wordpress 朋友圈
  • 后端八股之消息队列
  • Segment Anything: SAM SAM2
  • Oracle Linux 9 的 MySQL 8.0 完整安装与远程连接配置
  • 剑三做月饼活动网站网站制作公司司
  • 网站建设推广公司排名钓鱼链接生成器
  • 十字链表和邻接多重表
  • 中国排建设银行悦生活网站企业网站制作 深圳
  • Vue过度与动画
  • 陕西省高速建设集团公司网站商业空间设计书籍
  • 【快速入门】JMeter
  • 建立网站的基本条件免费广州网站开发维护
  • 【每天一个AI小知识】:什么是Prompt?