技术博客:构建一个响应式产品卡片

应用场景:

此产品卡片组件可用于各种电子商务或内容展示网站,为用户提供产品或内容的快速概览。

基本功能:

  • 展示产品或内容的图像、标题、颜色、价格等基本信息。
  • 提供“创建帖子”按钮,允许用户创建新内容。
  • 提供下拉菜单,包含“操作”、“另一个操作”、“此处还有其他内容”等选项。
  • 当用户将鼠标悬停在产品卡片上时,卡片会变为半透明状态。

功能实现步骤及关键代码分析:

1. 导入依赖项:

import { ref } from 'vue'

2. 定义数据:

使用 ref 响应式 API 定义一个包含产品信息的数组:

const data = ref([
  {
    id: 1,
    title: 'Basic Tee',
    color: 'Black',
    price: 30,
    image: 'https://source.unsplash.com/random/300x200'
  },
  // ... 其他产品信息
])

3. 创建卡片模板:

定义一个 Vue 模板,其中包含卡片的结构和内容:

<template>
  <div class="group relative">
    <div class="w-full min-h-80 bg-gray-200 aspect-w-1 aspect-h-1 rounded-md overflow-hidden group-hover:opacity-75 lg:h-80 lg:aspect-none">
      <img src="https://source.unsplash.com/random/300x200" alt="Front of men&#039;s Basic Tee in black." class="w-full h-full object-center object-cover lg:w-full lg:h-full">
    </div>
    <div class="mt-4 flex justify-between">
      <div>
        <h3 class="text-sm text-gray-700">
          <a href="#">
            <span aria-hidden="true" class="absolute inset-0"></span>
            Basic Tee
          </a>
        </h3>
        <p class="mt-1 text-sm text-gray-500">Black</p>
      </div>
      <p class="text-sm font-medium text-gray-900">$30.00</p>
    </div>
  </div>
</template>

4. 创建下拉菜单:

使用 relativeinline-block 类创建下拉菜单容器,并使用 aria-labelledby 属性将其与按钮关联:

<div class="relative inline-block text-left">
  <div>
    <button type="button" class="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" id="menu-button" aria-expanded="true" aria-haspopup="true">
      Options
      <svg class="-mr-1 ml-2 h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
        <path d="M10 6a2 2 0 110-4 2 2 0 010 4zM10 12a2 2 0 110-4 2 2 0 010 4zM10 18a2 2 0 110-4 2 2 0 010 4z" />
      </svg>
    </button>
  </div>
  <div class="origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 focus:outline-none" role="menu" aria-orientation="vertical" aria-labelledby="menu-button" tabindex="-1">
    <div class="py-1" role="none">
      <a href="#" class="text-gray-700 block px-4 py-2 text-sm" role="menuitem" tabindex="-1" id="menu-item-0">Action</a>
      <a href="#" class="text-gray-700 block px-4 py-2 text-sm" role="menuitem" tabindex="-1" id="menu-item-1">Another action</a>
      <a href="#" class="text-gray-700 block px-4 py-2 text-sm" role="menuitem" tabindex="-1" id="menu-item-2">Something else here</a>
    </div>
  </div>
</div>

5. 渲染卡片:

在 Vue 组件中使用 v-for 循环渲染产品数据并显示卡片:

<div class="grid grid-cols-1 gap-y-10 sm:grid-cols-2 sm:gap-x-6 lg:grid-cols-3 xl:grid-cols-4 xl:gap-x-8">
  <product-card v-for="item in data" :key="item.id" :product="item" />
</div>

总结与展望:

  • **开发经验:**开发此卡片组件让我对 Vue 响应式系统和 CSS 布局有了更深入的理解。
  • **收获:**我学习了如何创建响应式、可重用的组件,以及如何有效地使用下拉菜单。
  • **未来拓展:**该卡片组件未来可以进一步拓展,添加更多功能,例如:
    • 产品评论和评分系统
    • 购物车添加功能
    • 动态加载更多产品
Login
ECHO recommendation
ScriptEcho.ai

User Annotations

发现精彩游记