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

50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Form Wave(表单label波动效果)

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

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

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

在这里插入图片描述


🎯 组件目标

构建一个美观、动态的登录表单,重点在于实现带有浮动标签(floating label)的输入框体验,提升交互感知和视觉效果,适合作为任何登录注册模块的基础模板。

🛠️ 技术实现点

  • 使用 Vue3 + <script setup> 编写响应式逻辑。
  • 使用 TailwindCSS 完全控制样式,特别是浮动文字的动画。
  • 实现 focus/blur 状态下标签文字的动画浮动效果。
  • 使用 v-model 实现双向绑定,结合 focus 状态精准控制浮动逻辑。

🧱 组件实现

<!-- 🌈 模板部分 Template -->
<template><div class="flex h-screen items-center justify-center bg-gray-800 text-gray-300"><div class="rounded-2xl bg-gray-500/60 p-12 text-center"><h1 class="text-4xl font-bold text-gray-300">Please Login</h1><form><!-- Email 输入框 --><div class="form-control relative mt-10 border-b-2 border-b-white"><inputclass="peer relative z-10 w-full bg-transparent py-3 text-white focus:border-sky-300 focus:outline-none"type="text"requiredv-model="emailValue"@focus="activeInput = 'email'"@blur="handleBlur('email')" /><label class="pointer-events-none absolute top-4 left-0"><!-- ✨ 字符级浮动动画 --><spanv-for="(letter, idx) in 'Email'.split('')":key="idx":class="['inline-block min-w-[5px] text-lg transition-all duration-300','transform-gpu',{'-translate-y-8 text-sky-300':activeInput === 'email' || emailValue,},]":style="{transitionDelay: `${idx * 50}ms`,transitionTimingFunction: 'cubic-bezier(0.68, -0.55, 0.265, 1.55)',}">{{ letter }}</span></label></div><!-- Password 输入框 --><div class="form-control relative mt-10 border-b-2 border-b-white"><inputclass="peer relative z-10 w-full bg-transparent py-3 text-white focus:border-sky-300 focus:outline-none"type="password"requiredv-model="passwordValue"@focus="activeInput = 'password'"@blur="handleBlur('password')" /><label class="pointer-events-none absolute top-4 left-0"><!-- ✨ 字符级浮动动画 --><spanv-for="(letter, idx) in 'Password'.split('')":key="idx":class="['inline-block min-w-[5px] text-lg transition-all duration-300','transform-gpu',{'-translate-y-8 text-sky-300':activeInput === 'password' || passwordValue,},]":style="{transitionDelay: `${idx * 50}ms`,transitionTimingFunction: 'cubic-bezier(0.68, -0.55, 0.265, 1.55)',}">{{ letter }}</span></label></div><!-- 登录按钮 --><buttonclass="mt-10 w-full rounded bg-blue-500 px-4 py-2 font-bold hover:bg-blue-600 focus:ring-2 focus:ring-blue-400 focus:outline-none"type="submit">Login</button><p class="mt-10">Don't have an account?<a href="#" class="text-blue-400 hover:underline">Register</a></p></form></div></div>
</template>

🧩 重点效果实现

<!-- ⚙️ 脚本部分 Script -->
<script setup>
import { ref } from 'vue'// 输入值响应式变量
const emailValue = ref('')
const passwordValue = ref('')// 当前聚焦的输入项
const activeInput = ref(null)// 失焦逻辑:如果输入框为空,则取消浮动状态
const handleBlur = (inputName) => {if ((inputName === 'email' && !emailValue.value) ||(inputName === 'password' && !passwordValue.value)) {activeInput.value = null}
}
</script>
  • 标签浮动是通过 translate-y 配合 activeInput 或绑定值来实现的。
  • 使用 transition-delay 实现了字符级别的延迟动画,让文字一个个浮动。
  • 利用 cubic-bezier 定义自定义缓动函数,提升动画的弹性和自然感。

🎨 TailwindCSS 样式重点讲解

类名作用
peer / relative z-10确保 input 在 label 之上,供 label 状态判断使用
-translate-y-8控制文字上浮距离
transition-delay实现文字一个个浮动的动画延迟
transform-gpu使用 GPU 加速动画,提高性能和流畅度
focus:outline-none / focus:ring-2聚焦时视觉反馈
min-w-[5px]保证字符宽度一致,不会断行

🧾 常量定义 + 组件路由建议

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

{id: 8,title: 'Form Wave',image: 'https://50projects50days.com/img/projects-img/8-form-wave.png',link: 'FormWave',},

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

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

路由守卫可后续扩展身份验证逻辑,跳转到注册或首页。


🧠 小结

完成了通用场景下的表单样式界面,可以为你以后的表单设计以及登录页面提供一些灵感进行参考!!!🚀


👉 下一篇,我们将完成声音组件 Sound Board组件,可以实现点击发出对应的声音!🚀

http://www.xdnf.cn/news/10565.html

相关文章:

  • 力扣刷题(第四十五天)
  • navicate菜单栏不见了怎么办
  • cursor如何开启自动运行模式
  • PH热榜 | 2025-05-31
  • Docker常用命令详解与高效记忆指南
  • Android Studio历史版本下载地址汇总
  • 【软件测试】web自动化:Pycharm+Selenium+Firefox(一)
  • 【动画】unity中实现骨骼蒙皮动画
  • 使用FastAPI构建车牌检测识别服务
  • 「Python教案」字符串格式化操作
  • hooks组件-useState
  • 散列表(哈希表)
  • 函数调用的机器级实现(二):栈帧的访问与切换机制
  • 【笔记】为 Python 项目安装图像处理与科学计算依赖(MINGW64 环境)
  • 用wireshark抓包分析学习USB协议
  • 浅写弱口令与命令爆破
  • Cursor 编辑器介绍:专为程序员打造的 AI 编程 IDE
  • Python项目结构
  • 录屏不再难,从功能到体验深度测评
  • MPTCP 聚合吞吐
  • LRU和LFU缓存策略
  • ESP32系列AT固件快速开发——Wi-Fi MQTT
  • 【笔记】Windows系统部署suna基于 MSYS2的Poetry 虚拟环境backedn后端包编译失败处理
  • 汽车安全体系:FuSa、SOTIF、Cybersecurity 从理论到实战
  • 绿盟 IPS 设备分析操作手册
  • Nuxt3部署
  • TS 星际通信指南:从 TCP 到 UDP 的宇宙漫游
  • (Python)列表的操作(增删改查、排序)
  • 2025年ESWA SCI1区TOP,改进成吉思汗鲨鱼算法MGKSO+肝癌疾病预测,深度解析+性能实测
  • 网络攻防技术四:网络侦察技术