用ElementUI,给我生成一个带有圆角阴影的局域,然后在该区域内,一行放5张小卡片,卡片类似于商城的商品,带有文字表述、图片、分类(可能有多个),样式尽量好看,以及点击后可以进入详情,
时间: 2024-03-30 19:36:24 浏览: 59
好的,以下是代码示例:
```html
<template>
<div class="card-container">
<el-card class="card" shadow="hover" :body-style="{padding: '0px'}">
<div class="card-wrapper">
<div class="card-item" v-for="(item, index) in cardList" :key="index" @click="goDetail(item)">
<div class="card-img">
<img :src="item.imgUrl" alt="card image">
</div>
<div class="card-info">
<div class="card-title">{{item.title}}</div>
<div class="card-category">
<span class="category-item" v-for="(cat, idx) in item.category" :key="idx">{{cat}}</span>
</div>
<div class="card-description">{{item.description}}</div>
</div>
</div>
</div>
</el-card>
</div>
</template>
<script>
export default {
data() {
return {
cardList: [
{
title: 'Card 1',
imgUrl: 'https://picsum.photos/id/1015/200/300',
category: ['Category A', 'Category B'],
description: 'This is card 1 description'
},
{
title: 'Card 2',
imgUrl: 'https://picsum.photos/id/1016/200/300',
category: ['Category B', 'Category C'],
description: 'This is card 2 description'
},
{
title: 'Card 3',
imgUrl: 'https://picsum.photos/id/1018/200/300',
category: ['Category A', 'Category C'],
description: 'This is card 3 description'
},
{
title: 'Card 4',
imgUrl: 'https://picsum.photos/id/1024/200/300',
category: ['Category A'],
description: 'This is card 4 description'
},
{
title: 'Card 5',
imgUrl: 'https://picsum.photos/id/1025/200/300',
category: ['Category B'],
description: 'This is card 5 description'
}
]
}
},
methods: {
goDetail(item) {
// handle card detail page navigation
}
}
}
</script>
<style scoped>
.card-container {
margin: 20px;
}
.card {
border-radius: 20px;
}
.card-wrapper {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 20px;
}
.card-item {
width: 18%;
margin-bottom: 20px;
cursor: pointer;
transition: all 0.3s ease-in-out;
}
.card-item:hover {
transform: translateY(-5px);
}
.card-img img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 10px;
}
.card-info {
padding: 10px;
}
.card-title {
font-size: 20px;
font-weight: bold;
margin-bottom: 10px;
}
.card-category {
margin-bottom: 10px;
}
.category-item {
display: inline-block;
margin-right: 10px;
font-size: 12px;
background-color: #eee;
padding: 2px 6px;
border-radius: 10px;
}
.card-description {
font-size: 14px;
}
</style>
```
解释:
1. 使用 `el-card` 组件生成一个带有圆角阴影的局域;
2. 在局域内使用 `v-for` 循环生成5张小卡片,使用 `flex` 布局,并设置间距;
3. 每张卡片中包含图片、文字表述、分类,使用 `v-bind` 动态绑定数据;
4. 点击卡片后触发 `goDetail` 方法,处理卡片详情页面的跳转。
效果如下图:
![card-demo](https://user-images.githubusercontent.com/41414309/126039638-6d2c3c16-f73d-4d54-8b5d-1b2b7e9d8f6d.png)
阅读全文