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

50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Blurry Loading (毛玻璃加载)

📅 我们继续 50 个小项目挑战!—— Blurry Loading 组件

  • 仓库地址:https://github.com/SunACong/50-vue-projects

  • 项目预览地址:https://50-vue-projects.vercel.app/

在这里插入图片描述


✨ 组件目标

  • 实现一个加载进度条,随着加载进度的增加,背景图像逐渐从模糊变清晰

  • 展示一个百分比数字,表示当前的加载进度

  • 整个过程无需外部库,完全依赖 Vue3 和 Tailwind CSS


🧱 技术实现点

  • Vue3 的响应式状态管理(ref)

  • 使用 onMounted 和 onBeforeUnmount 生命周期钩子管理定时器

  • Tailwind CSS 的 absolute、inset-0、bg-cover、bg-center 等布局类

  • 动态绑定内联样式,实现模糊效果的渐变

🔧 BlurryLoading.vue 组件实现

<template><div class="relative h-screen w-screen"><div:style="{ filter: `blur(${blurPx}px)` }"class="absolute inset-0 bg-[url('https://images.unsplash.com/photo-1576161787924-01bb08dad4a4?auto=format&fit=crop&w=2104&q=80')] bg-cover bg-center bg-no-repeat"></div><div class="absolute inset-0 flex items-center justify-center"><div class="text-5xl font-bold text-gray-300">{{ loading }}%</div></div></div>
</template><script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'const loading = ref(0)
const blurPx = ref(30)let intervalonMounted(() => {interval = setInterval(() => {if (loading.value < 100) {loading.value += 2blurPx.value = 30 * (1 - loading.value / 100)} else {clearInterval(interval)}}, 30)
})onBeforeUnmount(() => {clearInterval(interval)
})
</script>

⭐ 渐显效果

  • ref 变量 opacity,根据 loading.value 动态变化,随着加载进度的推进从 0 线性增长到 1
  • 配合 transition-opacity duration-500 的 Tailwind 类,使背景图从完全透明渐显到完全不透明。
  • 为任何元素设置 :style="{ opacity: xxx }" 配合 Tailwind 的过渡类,都可以实现渐显。

💡 TailwindCSS 样式重点讲解

类名功能描述
absolute inset-0使元素绝对定位并填满父容器
bg-cover背景图像覆盖整个容器
bg-[url(xxx)]设置背景图像
bg-center背景图像居中显示
bg-no-repeat背景图像不重复
text-5xl设置字体大小为 5xl
font-bold设置字体加粗
text-gray-300设置字体颜色为灰色(300)

🦌 常量定义 + 组件路由

constants/index.js 添加组件预览常量:

export const projectList = [
{id: 5,title: 'Blurry Loading',image: 'https://50projects50days.com/img/projects-img/5-blurry-loading.png',link: 'BlurryLoading',}
]

router/index.js 中添加路由选项:

{path: '/BlurryLoading',name: 'BlurryLoading',component: () => import('@/projects/BlurryLoading.vue')
}

🚀 小结

这个组件展示了如何结合 Vue3 的响应式特性和 Tailwind CSS 的实用工具类,实现一个动态的加载效果。通过动态调整背景图像的模糊程度,提升了用户体验。

📅 明日预告:Scroll Animation!实现滚动动画组件。

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

相关文章:

  • C#中的ThreadStart委托
  • GPU加速Kubernetes集群助力音视频转码与AI工作负载扩展
  • LeetCode[222]完全二叉树的节点个数
  • DPDK 技术详解:榨干网络性能的“瑞士军刀”
  • anaconda的c++环境与ros2需要的系统变量c++环境冲突
  • Docker 疑难杂症解决指南大纲
  • 深入解析Spring Boot与Kafka集成:构建高效消息驱动微服务
  • Python 实现web请求与响应
  • 演示:【WPF-WinCC3D】 3D工业组态监控平台源代码
  • 【PostgreSQL数据分析实战:从数据清洗到可视化全流程】1.4 数据库与表的基本操作(DDL/DML语句)
  • CUDA加速的线性代数求解器库cuSOLVER
  • Oracle 物理存储与逻辑管理
  • vscode优化使用体验篇(快捷键)
  • 如何在电脑上登录多个抖音账号?多开不同IP技巧分解
  • 【东枫科技】usrp rfnoc 开发环境搭建
  • 【JAVA资料,C#资料,人工智能资料,Python资料】全网最全编程学习文档合集,从入门到全栈,保姆级整理!
  • [IMX] 05.串口 - UART
  • 使用Tkinter写一个发送kafka消息的工具
  • MongoDB 与 EF Core 深度整合实战:打造结构清晰的 Web API 应用
  • JAVA|后端编码规范
  • 重写B站(网页、后端、小程序)
  • 文档债务拖累交付速度?5大优化策略文档自动化
  • 【数据结构与算法】LeetCode 每日三题
  • 基于深度学习的电力负荷预测研究
  • 篇章十 消息持久化(二)
  • 【IDEA】删除/替换文件中所有包含某个字符串的行
  • 基于深度学习的不良驾驶行为为识别检测
  • FD+Mysql的Insert时的字段赋值乱码问题
  • 第十周作业
  • Python操作PDF书签详解 - 添加、修改、提取和删除