该段代码用于构建一个图片列表展示界面,其中每张图片包含标题、描述和操作按钮。该界面常用于电商网站、博客或画廊等需要展示大量图片的场景中。
<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>
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
循环渲染图片列表。group
和 group-hover
类用于实现鼠标悬停显示操作按钮的效果。absolute
和 inset-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;
}
关键代码分析: