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

50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GoodCheapFast(Good - Cheap - Fast三选二开关)

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

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

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

在这里插入图片描述


使用 Vue 3 的 Composition API 和 TailwindCSS 创建一个经典的交互式组件 —— “Good - Cheap - Fast” 三选一开关控制。这个组件模拟了项目开发中常见的权衡原则:你只能同时拥有其中两个选项。通过点击任意一个开关,用户可以动态切换状态,并自动关闭其他其中一个已开启的选项。


🎯 组件目标

  • 实现“三选二”的互斥选择逻辑
  • 提供美观的自定义开关按钮样式(基于 TailwindCSS)
  • 使用 Vue 3 Composition API 管理响应式状态与交互逻辑
  • 支持清晰的 UI 反馈(颜色、文本提示)

⚙️ 技术实现点

技术点描述
Vue 3 <script setup>使用 ref 管理三个布尔状态(good、cheap、fast)
自定义函数 toggleFeature控制开关行为并确保最多只开启两个选项
TailwindCSS 样式构建现代感十足的开关组件和布局
动态类绑定根据开关状态动态调整颜色和位置

🧱 组件实现

模板结构 <template>

<template><div class="flex min-h-screen items-center justify-center bg-gray-100"><div class="w-96 rounded-lg bg-white p-8 shadow-lg"><h1 class="mb-6 text-center text-2xl font-bold">Good - Cheap - Fast 三选一</h1><div class="space-y-4"><!-- 高质量 --><div class="flex items-center justify-between"><span class="text-gray-700">高质量 (Good)</span><label class="flex cursor-pointer items-center"><input type="checkbox" class="hidden" :checked="good" @change="toggleFeature('good')" /><div :class="['h-6 w-12 rounded-full bg-gray-300 p-1 transition-all', good ? 'bg-green-500' : '']"><div :class="['h-4 w-4 rounded-full bg-white transition-all', good ? 'translate-x-6' : '']"></div></div><span :class="['ml-2 text-sm font-medium', good ? 'text-green-500' : 'text-gray-500']">{{ good ? 'ON' : 'OFF' }}</span></label></div><!-- 低成本 --><div class="flex items-center justify-between"><span class="text-gray-700">低成本 (Cheap)</span><label class="flex cursor-pointer items-center"><input type="checkbox" class="hidden" :checked="cheap" @change="toggleFeature('cheap')" /><div :class="['h-6 w-12 rounded-full bg-gray-300 p-1 transition-all', cheap ? 'bg-green-500' : '']"><div :class="['h-4 w-4 rounded-full bg-white transition-all', cheap ? 'translate-x-6' : '']"></div></div><span :class="['ml-2 text-sm font-medium', cheap ? 'text-green-500' : 'text-gray-500']">{{ cheap ? 'ON' : 'OFF' }}</span></label></div><!-- 快速交付 --><div class="flex items-center justify-between"><span class="text-gray-700">快速交付 (Fast)</span><label class="flex cursor-pointer items-center"><input type="checkbox" class="hidden" :checked="fast" @change="toggleFeature('fast')" /><div :class="['h-6 w-12 rounded-full bg-gray-300 p-1 transition-all', fast ? 'bg-green-500' : '']"><div :class="['h-4 w-4 rounded-full bg-white transition-all', fast ? 'translate-x-6' : '']"></div></div><span :class="['ml-2 text-sm font-medium', fast ? 'text-green-500' : 'text-gray-500']">{{ fast ? 'ON' : 'OFF' }}</span></label></div></div></div></div>
</template>

该模板构建了一个简洁而直观的界面,每个功能项都包含一个标签和一个自定义样式的开关控件,支持用户交互。


脚本逻辑 <script setup>

<script setup>
import { ref } from 'vue'// 初始化三个开关状态
const good = ref(false)
const cheap = ref(false)
const fast = ref(false)// 计算当前已开启的功能数量
const getActiveCount = () => {let count = 0if (good.value) count++if (cheap.value) count++if (fast.value) count++return count
}// 实现切换功能的方法
const toggleFeature = (feature) => {switch (feature) {case 'good':// 如果当前已经有两个开启,且要开启 good,则需要关闭一个if (!good.value && getActiveCount() >= 2) {// 关闭第一个开启的其他功能if (cheap.value) cheap.value = falseelse if (fast.value) fast.value = false}good.value = !good.valuebreakcase 'cheap':if (!cheap.value && getActiveCount() >= 2) {if (good.value) good.value = falseelse if (fast.value) fast.value = false}cheap.value = !cheap.valuebreakcase 'fast':if (!fast.value && getActiveCount() >= 2) {if (good.value) good.value = falseelse if (cheap.value) cheap.value = false}fast.value = !fast.valuebreak}
}
</script>

脚本部分主要处理开关的逻辑判断和状态管理,确保始终只有最多两个功能处于开启状态。


🎨 TailwindCSS 样式重点讲解

类名作用
flex, min-h-screen, items-center, justify-center创建全屏垂直居中的布局
bg-gray-100设置背景颜色为浅灰色,增加视觉层次感
rounded-lg, bg-white, p-8, shadow-lg定义卡片样式的边框圆角、内边距及阴影效果
text-2xl, font-bold, text-center标题样式设置
space-y-4控制每组开关之间的垂直间距
cursor-pointer当鼠标悬停在开关上时显示为手形光标
transition-all添加平滑的动画过渡效果
translate-x-6控制开关内部圆形滑块的位置偏移,表示 ON 状态
bg-green-500, text-green-500表示开启状态下的颜色反馈

这些 TailwindCSS 类共同构建出一个现代风格的交互组件。


🔍 关键功能解析

✅ 三选二逻辑实现

核心在于 toggleFeature 函数,它根据当前激活的开关数量进行判断:

  • 如果用户尝试打开第三个选项,将自动关闭最早开启的一个。
  • 这种互斥机制保证了“Good - Cheap - Fast”模型的核心原则始终成立。

💡 用户体验优化

  • 开关状态以颜色变化(绿色/灰色)和文字提示(ON/OFF)即时反馈给用户。
  • 滑动动画增强交互的真实感和趣味性。

📁 常量定义 + 组件路由

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

{id: 32,title: 'Good Cheap Fast',image: 'https://50projects50days.com/img/projects-img/32-good-cheap-fast.png',link: 'GoodCheapFast',},

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

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

🏁 总结

使用 Vue 3 Composition API 和 TailwindCSS 实现了一个经典的“Good - Cheap - Fast”三选二开关组件。通过合理的状态管理和优雅的样式设计,我们不仅实现了功能上的需求,还提供了良好的用户体验。

你可以扩展以下功能:

  • ✅ 添加复位按钮:一键关闭所有开关。
  • ✅ 记录历史状态:让用户查看之前的选择组合。
  • ✅ 加入图标或图片:提升视觉表达力。

👉 下一篇,我们将完成NoteApp组件,实现了小便签笔记的组件,可以对便签增删改查,支持MarkDown语法解析。🚀

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

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

相关文章:

  • 区块链之Casper协议背景、演变发展、运作机制和潜在风险
  • 周志华《机器学习导论》第8章 集成学习 Ensemble Learning
  • 2025开源组件安全工具推荐OpenSCA
  • LVS(Linux virtual server)
  • AWS Lambda 最佳实践:构建高效无服务器应用的完整指南
  • 多维动态规划题解——最长公共子序列【LeetCode】记忆化搜索翻译成递推
  • CCS缺陷|冻干/灌装10大缺陷暴露无菌生产系统性漏洞:气流流型缺陷
  • 【android bluetooth 协议分析 03】【蓝牙扫描详解 3】【Bluetooth 中 EIR、IR、BLE 普通广播与扩展广播详解】
  • 数仓建设中,系统数据录入错误或者延迟,如何对历史数据修复或补入?
  • 安装物理机ubuntu系统
  • 初试Spring AI实现聊天功能
  • PyCharm 高效入门指南(引言 + 核心模块详解)
  • ubuntu22 npm install electron --save-dev 失败
  • 基于Rust游戏引擎实践(Game)
  • 智能体开发工具链全景图:IDE、调试器与监控平台
  • 营业执照识别-营业执照文字识别API-营业执照真伪
  • Flutter状态管理篇之ChangeNotifier(一)
  • 什么是协变(Covariant)与逆变(Contravariant)?
  • 在 Windows Server RDS 上配置用户配置文件磁盘查找对应的用户名
  • 第2章通用的高并发架构设计——2.5 高并发读场景总结:CQRS
  • 【计算机网络】第四章:网络层(上)
  • Spring中事务失效的情况深度分析
  • 深入理解 KVM 子系统:从虚拟化核心到内核实现的全景解析
  • 7.18 note
  • 第十八节:第三部分:java高级:反射-获取构造器对象并使用
  • 【参数初始化】——深度学习.全连接网络
  • Cadence SPB 2024软件下载及安装教程|Cadence软件安装详细步骤附下载链接
  • Spring IoCDI_2_使用篇
  • Anime.js 超级炫酷的网页动画库详解
  • 【图像质量评价指标】信噪比(Signal-to-Noise Ratio,SNR)