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

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.xdnf.cn/news/15452.html

相关文章:

  • Android事件分发机制完整总结
  • 快速搭建Maven仓库服务
  • 深入理解 Linux 文件系统层级结构
  • 深入理解 Java JVM
  • Clojure和Golang中的Channel有什么异同(TBC)
  • AI驱动的软件工程(中):文档驱动的编码与执行
  • Python协程进阶:优雅终止与异常处理详解
  • python代码块的表示方法
  • 输入npm install后发生了什么
  • Maven 构建命令
  • HTML 基本骨架
  • 【LeetCode 热题 100】23. 合并 K 个升序链表——(解法一)逐一合并
  • DOS下EXE文件的分析 <1>
  • Linux锁的概念及线程同步
  • 【iOS】方法与消息底层分析
  • 深入了解JAVA中Synchronized
  • CCS-MSPM0G3507-7-模块篇-MPU6050的基本使用
  • 002大模型基础知识
  • 认识String、StringBuffer、StringBuilder
  • vue3 el-select默认选中
  • 【设计模式】策略模式(政策(Policy)模式)
  • 从 Manifest V2 升级到 Manifest V3 的注意事项
  • Cursor三大核心AI功能
  • 详解缓存淘汰策略:LFU
  • JS红宝书pdf完整版
  • mac上BRPC的CMakeLists.txt优化:解决Protobuf路径问题
  • SCTP协议网络编程
  • 【算法】贪心算法:柠檬水找零C++
  • Redis 命令总结
  • SpringBoot3-Flowable7初体验