50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Expanding Cards (展开式卡片)

📅 我们继续 50 个小项目挑战!------ Expanding Cards 组件

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

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


✨ 组件目标

  • 一组横向排列的背景卡片
  • 点击卡片后,其展开至主要区域,其他卡片收缩
  • 显示当前卡片的标题,其他隐藏

🧱 技术实现点

  • Vue3 的响应式状态 ref
  • v-for 渲染卡片列表
  • TailwindCSS flex, transition, bg-cover, rounded, bg-[url] 等核心工具类
  • 条件样式绑定 :class

🔧 ExpandingCards.vue 组件实现

html 复制代码
<template>
  <div class="flex min-h-screen justify-center p-20">
    <div
      v-for="item in imagList"
      :key="item.id"
      @click="current = item.id"
      :class="[
        'relative m-2 flex-1/12 cursor-pointer rounded-[4vw] bg-cover bg-center bg-no-repeat font-mono text-2xl text-white transition-all duration-500 ease-in-out',
        item.className,
        current === item.id ? 'flex-2/3' : 'flex-1/12',
      ]">
      <h3
        :class="[
          'absolute bottom-5 left-15 transition-all duration-500 ease-in-out',
          current === item.id ? 'opacity-100' : 'opacity-0',
        ]">
        {{ item.title }}
      </h3>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const current = ref(1)

const imagList = [
  {
    id: 1,
    className:
      'bg-[url(https://images.unsplash.com/photo-1558979158-65a1eaa08691?auto=format&fit=crop&w=1350&q=80)]',
    title: 'Explore The World',
  },
  {
    id: 2,
    className:
      'bg-[url(https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?auto=format&fit=crop&w=1350&q=80)]',
    title: 'Wild Forest',
  },
  {
    id: 3,
    className:
      'bg-[url(https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=1353&q=80)]',
    title: 'Sunny Beach',
  },
  {
    id: 4,
    className:
      'bg-[url(https://images.unsplash.com/photo-1551009175-8a68da93d5f9?auto=format&fit=crop&w=1351&q=80)]',
    title: 'City on Winter',
  },
  {
    id: 5,
    className:
      'bg-[url(https://images.unsplash.com/photo-1549880338-65ddcdfd017b?auto=format&fit=crop&w=1350&q=80)]',
    title: 'Mountains - Clouds',
  },
]
</script>

💡 TailwindCSS 样式重点讲解

类名 功能描述
flex-1/12, flex-2/3 自定义比例控制卡片展开宽度
bg-cover bg-center 背景图片尺寸与居中展示
rounded-[4vw] 圆角自定义为视口宽度
transition-all duration-500 平滑过渡效果
opacity-0 / opacity-100 控制标题显隐

🦌 常量定义 + 组件路由

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

js 复制代码
export const projectList = [
    {
        id: 1,
        title: 'Expanding Cards',
        image: 'https://50projects50days.com/img/projects-img/1-expanding-cards.png',
        link: 'ExpandingCards',
    }
]

router/index.jsrouter对象添加路由选项

js 复制代码
{
        path: '/ExpandingCards',
        name: 'ExpandingCards',
        component: () => import('@/projects/ExpandingCards.vue')
    }

🚀 小结

这个组件展示了如何用非常少的代码和工具类实现炫酷 UI 效果。我们学会了:

  • 使用 v-for 构建动态组件结构
  • ref 管理当前状态
  • 动态绑定 class 实现动画和交互反馈
  • 熟练使用 TailwindCSS v4 的实用工具类,提升开发效率

📅 明日预告:Progress Steps!实现步骤引导组件。


🧠 每天进步一点点,50 天后惊艳所有人!

相关推荐
大橙子额1 天前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
WooaiJava1 天前
AI 智能助手项目面试技术要点总结(前端部分)
javascript·大模型·html5
LYFlied1 天前
从 Vue 到 React,再到 React Native:资深前端开发者的平滑过渡指南
vue.js·react native·react.js
Never_Satisfied1 天前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
董世昌411 天前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
B站_计算机毕业设计之家1 天前
豆瓣电影数据采集分析推荐系统 | Python Vue Flask框架 LSTM Echarts多技术融合开发 毕业设计源码 计算机
vue.js·python·机器学习·flask·echarts·lstm·推荐算法
WeiXiao_Hyy1 天前
成为 Top 1% 的工程师
java·开发语言·javascript·经验分享·后端
xjt_09011 天前
基于 Vue 3 构建企业级 Web Components 组件库
前端·javascript·vue.js
我是伪码农1 天前
Vue 2.3
前端·javascript·vue.js
辰风沐阳1 天前
JavaScript 的宏任务和微任务
javascript