代码应用场景

该段代码用于构建一个图片列表展示界面,其中每张图片包含标题、描述和操作按钮。该界面常用于电商网站、博客或画廊等需要展示大量图片的场景中。

代码基本功能

  • 展示多张图片,每张图片包含标题、描述和操作按钮。
  • 鼠标悬停在图片上时,显示操作按钮。
  • 单击操作按钮时,执行相应操作(例如查看详情、添加到购物车等)。

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

HTML 结构

<div class="grid grid-cols-1 gap-y-10 sm:grid-cols-2 gap-x-6 lg:grid-cols-3 xl:grid-cols-4 xl:gap-x-8">
  <!-- 图片列表 -->
</div>

JavaScript 代码

const listData = ref([
  {
    title: 'Ant Design Title 1',
    description: 'Ant Design Description 1',
  },
  {
    title: 'Ant Design Title 2',
    description: 'Ant Design Description 2',
  },
]);

关键代码分析

图片列表循环渲染:

<template v-for="item in listData" :key="item.title">
  <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/300x300" alt="" 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>
            {{ item.title }}
          </a>
        </h3>
        <p class="mt-1 text-sm text-gray-500">{{ item.description }}</p>
      </div>
      <!-- 操作按钮 -->
      <p class="text-sm font-medium text-gray-900">11.49</p>
    </div>
    <!-- 悬停时显示操作按钮 -->
    <div class="absolute inset-0 px-4 py-4 sm:px-6 sm:py-5">
      <div class="text-sm font-medium text-white">New</div>
    </div>
    <div class="absolute inset-0 bg-black bg-opacity-40 group-hover:opacity-0">
      <div class="flex items-center justify-center">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
          <path stroke-linecap="round" stroke-linejoin="round" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z" />
        </svg>
      </div>
    </div>
  </div>
</template>

关键代码分析:

  • v-for 循环渲染图片列表。
  • groupgroup-hover 类用于实现鼠标悬停显示操作按钮的效果。
  • absoluteinset-0 类用于设置操作按钮的绝对定位和全屏覆盖效果。
  • bg-black bg-opacity-40 类设置操作按钮的黑色背景和 40% 透明度。
  • group-hover:opacity-0 类设置操作按钮在鼠标悬停时隐藏。

样式代码

.text-sm {
  font-size: 0.875rem;
}

.text-gray-700 {
  color: var(--color-dark);
}

.a-link {
  color: var(--color-primary);
  text-decoration: none;
}

.a-link:hover {
  text-decoration: underline;
}

.mt-1 {
  margin-top: 4px;
}

.text-sm {
  font-size: 0.875rem;
}

.text-gray-500 {
  color: var(--color-tertiary);
}

.text-sm {
  font-size: 0.875rem;
}

.font-medium {
  font-weight: 500;
}

.text-gray-900 {
  color: var(--color-dark);
}

.absolute {
  position: absolute;
}

.inset-0 {
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.px-4 {
  padding-left: 16px;
  padding-right: 16px;
}

.py-4 {
  padding-top: 16px;
  padding-bottom: 16px;
}

.sm\:px-6 {
  padding-left: 24px;
  padding-right: 24px;
}

.sm\:py-5 {
  padding-top: 20px;
  padding-bottom: 20px;
}

.text-sm {
  font-size: 0.875rem;
}

.font-medium {
  font-weight: 500;
}

关键代码分析:

  • 设置文本颜色、大小和字体加粗。
  • 设置元素的内边距和外边距。
  • 设置元素的绝对定位和全屏覆盖效果。

总结与展望

开发过程中的经验与收获

  • 掌握了 Vue.js 中 v-for 循环、group 和 absolute 等指令的使用。
  • 深入理解了 CSS 中绝对定位和覆盖效果的实现。
  • 提升了对响应式布局的应用能力。

未来该卡片功能的拓展与优化

  • **动态加载数据:**通过 API 请求从后端加载图片列表数据,实现无限滚动或分页加载。
  • **图片懒加载:**使用懒加载技术,仅在图片进入可视区域时才加载,优化页面性能。
  • **图片裁剪:**根据不同设备屏幕尺寸,自动裁剪图片以适应最佳展示效果。
  • **图片滤镜:**添加图片滤镜功能,允许用户对图片进行滤镜处理。
  • **图片上传:**允许用户上传自己的图片并将其添加到列表中。
Login
ECHO recommendation
ScriptEcho.ai
User Annotations

列表