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

50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ToastNotification(推送通知)

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

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

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

在这里插入图片描述


使用 Vue 3 的 Composition API(<script setup>)结合 TailwindCSS 创建一个带动画效果的通知提示组件(Toast)。该组件支持点击按钮显示通知,并会在 3 秒后自动消失。


🎯 组件目标

  • 点击按钮时显示一条通知
  • 每条通知独立显示并带有入场和出场动画
  • 通知在 3 秒后自动消失
  • 使用 Vue 响应式变量管理通知列表
  • 使用 TailwindCSS 构建 UI 样式与动画
  • 支持多个通知依次排列展示

⚙️ 技术实现点

技术点描述
Vue 3 <script setup>使用响应式变量管理通知列表
ref 响应式变量控制通知数组 notifications 和自增 ID
v-for 渲染通知列表动态渲染每一条通知
setTimeout 定时器控制通知自动消失时间
<transition-group>添加入场和出场动画
TailwindCSS 动画类实现平滑过渡效果
TailwindCSS 布局类设置通知容器位置和样式

🧱 组件实现

模板结构 <template>

<template><div class="flex h-screen items-center justify-center"><buttonclass="rounded-md bg-white px-4 py-2 font-mono text-black shadow hover:bg-gray-100"@click="showNotification">Show Notification</button><!-- 通知容器 --><div class="fixed right-4 bottom-4 flex flex-col items-end space-y-6"><transition-grouptag="div"enter-active-class="transition transform ease-out duration-300"enter-from-class="opacity-0 translate-y-6"enter-to-class="opacity-100 translate-y-0"leave-active-class="transition transform ease-in duration-300"leave-from-class="opacity-100 translate-y-0"leave-to-class="opacity-0 -translate-y-6"><divv-for="note in notifications":key="note.id"class="mt-4 min-w-[220px] rounded bg-white px-4 py-3 text-sm text-gray-800 shadow-lg ring-1 ring-gray-200">{{ note.text }}</div></transition-group></div></div>
</template>

脚本逻辑 <script setup>

<script setup>
import { ref } from 'vue'const notifications = ref([])
let idCounter = 0function showNotification() {const id = idCounter++notifications.value.push({id,text: `🔔 Notification #${id}`,})// 3 秒后自动移除setTimeout(() => {notifications.value = notifications.value.filter((n) => n.id !== id)}, 3000)
}
</script>

🔍 重点效果实现

✅ 动态通知列表管理

使用 ref 来维护通知列表:

const notifications = ref([])
let idCounter = 0

每次点击按钮都会生成一个新的通知对象,并赋予唯一的 id

💡 自动隐藏通知

通过 setTimeout 在 3 秒后从列表中移除指定通知:

setTimeout(() => {notifications.value = notifications.value.filter((n) => n.id !== id)
}, 3000)

这样可以保证旧的通知不会堆积在页面上。

🎨 入场 & 出场动画

使用 <transition-group> 组件配合 TailwindCSS 的过渡类来实现动画效果:

<transition-grouptag="div"enter-active-class="transition transform ease-out duration-300"enter-from-class="opacity-0 translate-y-6"enter-to-class="opacity-100 translate-y-0"leave-active-class="transition transform ease-in duration-300"leave-from-class="opacity-100 translate-y-0"leave-to-class="opacity-0 -translate-y-6">

实现了:

  • 入场:从透明向下移动到可视区域
  • 出场:从可视区域向上淡出消失

视觉上非常自然流畅。


🎨 TailwindCSS 样式重点讲解

类名作用
flex h-screen items-center justify-center居中按钮
fixed right-4 bottom-4固定右下角定位通知容器
flex flex-col items-end右对齐排列通知
min-w-[220px], rounded, shadow-lg通知基础样式
text-sm, text-gray-800文字颜色与大小
ring-1 ring-gray-200添加浅色边框环
transition transform ease-out duration-300过渡动画控制
opacity-0 translate-y-6初始状态为透明并偏移下方
opacity-100 translate-y-0显示状态为不透明并居中

这些 TailwindCSS 类帮助我们快速构建了一个美观、功能完整的通知组件。


📁 数据结构建议(可扩展)

你可以将通知数据结构封装成函数或服务模块,便于复用和测试:

// services/notification.js
export function createNotification(id, message = `🔔 Notification #${id}`) {return {id,text: message,}
}

并在组件中调用:

import { createNotification } from '@/services/notification'notifications.value.push(createNotification(idCounter++))

📁 常量定义 + 组件路由

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

{id: 27,title: 'Toast Notification',image: 'https://50projects50days.com/img/projects-img/27-toast-notification.png',link: 'ToastNotification',},

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

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

🏁 总结

通知组件不仅实现了基本的提示功能,还展示了如何使用 Vue 的响应式能力和 <transition-group> 实现动画化的交互体验,用于展示操作反馈、错误提示或成功消息等。

你可以进一步扩展此组件的功能包括:

  • ✅ 添加关闭按钮手动关闭通知
  • ✅ 支持不同类型的提示(success / warning / error)
  • ✅ 支持从底部弹出、顶部滑入等多种动画方向
  • ✅ 封装为全局插件,如 app.use(NotificationPlugin)
  • ✅ 将组件封装为 <AppNotification /> 可复用组件

👉 下一篇,我们将完成Github Profiles组件,可以获取github个人信息并展示为卡片消息。🚀

感谢阅读,欢迎点赞、收藏和分享 😊

http://www.dtcms.com/a/276903.html

相关文章:

  • C++进阶-多态2
  • 学习python调用WebApi的基本用法(2)
  • iw 命令 -- linux 无线管理
  • 利用 MySQL 进行数据清洗
  • C++类和对象(一)
  • Intel英特尔ICH7R/ICH8R/ICH9R/ICH10R系列下载地址--intel_msm_8961002 下载 Version 8.9.6.1002
  • 001_Claude开发者指南介绍
  • UNet改进(22):融合CNN与Transformer的医学图像分割新架构
  • MaxCompute过程中常见的数据倾斜场景以及对应的解决方案
  • std::sort的核心设计思想
  • C++:宏
  • python暑假课第三次作业
  • 从爆红到跑路:AI明星Manus为何仅用四个月就“抛弃”了中国?
  • 详解缓存淘汰策略:LFU
  • macOS - Chrome 关闭自动更新
  • 12.1 MMU配置与管理
  • 人工智能之数学基础:神经网络的矩阵参数求导
  • 基于CMMI的软件质量管理体系深度解析
  • 初级网安作业笔记1
  • 2025上海市“星光计划“信息安全管理与评估赛项二三阶段任务书
  • 【leetcode】字符串,链表的进位加法与乘法
  • 贝叶斯状态空间神经网络:融合概率推理和状态空间实现高精度预测和可解释性
  • 新手向:使用Python构建高效的日志处理系统
  • Linux系统之iprdbg 命令详解
  • 12.4 内存隔离与保护
  • 《Llama: The Llama 3 Herd of Models》预训练数据篇——论文精读笔记
  • Linux | 数据库操作基础
  • EVO-0:具有隐空间理解的视觉-语言-动作模型
  • 维基艺术图片: 构建模型 (3)
  • 应用层协议和JSON的使用