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

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.lqws.cn/news/73189.html

相关文章:

  • 自主设计一个DDS信号发生器
  • 每天掌握一个Linux命令 - hping3
  • 工作流引擎-16-开源审批流项目之 整合Flowable官方的Rest包
  • NiceGUI 是一个基于 Python 的现代 Web 应用框架
  • Windows10-ltsc-2019 使用 PowerShell 安装安装TranslucentTB教程(不通过微软商店安装)
  • Qt概述:基础组件的使用
  • 动态类型语言和静态类型语言
  • 【MySQL基础】库的操作:创建、删除与管理数据库
  • [ Qt ] | 与系统相关的操作(一):鼠标相关事件
  • 分布式锁优化:使用Lua脚本保证释放锁的原子性问题
  • 网络安全的学习路线是怎么样的?
  • 【C语言】C语言经典小游戏:贪吃蛇(下)
  • 用 Whisper 打破沉默:AI 语音技术如何重塑无障碍沟通方式?
  • 【iOS】YYModel源码解析
  • Git GitHub Gitee
  • ISBN书号查询接口如何用PHP实现调用?
  • 房屋租赁系统 Java+Vue.js+SpringBoot,包括房屋类型、房屋信息、预约看房、合同信息、房屋报修、房屋评价、房主管理模块
  • Python训练营打卡 Day26
  • JavaScript性能优化:实战技巧提升10倍速度
  • 2025年—Comfy UI 和 Stable Diffusion底层原理
  • docker可视化工具
  • 【头歌实验】Keras机器翻译实战
  • volatile,synchronized,原子操作实现原理,缓存一致性协议
  • 【JAVA后端入门基础001】Tomcat 是什么?通俗易懂讲清楚!
  • 类和对象:实现日期类
  • Windows采用npx方式本地部署n8n
  • Linux(信号)
  • 并发编程的源头
  • 探索 Dify 的工作流:构建智能应用的新范式
  • Flink CDC将MySQL数据同步到数据湖