vue多排轮播图代码
时间: 2023-03-05 13:11:15 浏览: 65
以下是一个基于Vue.js实现的多排轮播图的代码示例:
HTML模板:
```
<template>
<div class="carousel-container">
<div v-for="row in rows" :key="row">
<div class="carousel-row">
<div v-for="(item, index) in items" :key="index" class="carousel-item">
<img :src="item.image" alt="carousel item" />
<div class="item-caption">{{ item.caption }}</div>
</div>
</div>
</div>
</div>
</template>
```
CSS样式:
```
.carousel-container {
display: flex;
flex-direction: column;
align-items: center;
overflow: hidden;
}
.carousel-row {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
}
.carousel-item {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 0 10px;
width: 200px;
height: 200px;
background-color: #f0f0f0;
border-radius: 10px;
}
.item-caption {
margin-top: 10px;
font-size: 16px;
font-weight: bold;
}
```
JavaScript代码:
```
<script>
export default {
data() {
return {
items: [
{
image: 'https://via.placeholder.com/200x200?text=Item+1',
caption: 'Item 1',
},
{
image: 'https://via.placeholder.com/200x200?text=Item+2',
caption: 'Item 2',
},
{
image: 'https://via.placeholder.com/200x200?text=Item+3',
caption: 'Item 3',
},
{
image: 'https://via.placeholder.com/200x200?text=Item+4',
caption: 'Item 4',
},
{
image: 'https://via.placeholder.com/200x200?text=Item+5',
caption: 'Item 5',
},
],
rows: [],
};
},
mounted() {
const numRows = Math.ceil(this.items.length / 3);
for (let i = 0; i < numRows; i++) {
this.rows.push(i);
}
},
};
</script>
```
这个代码实现了一个每行有三个元素的多排轮播图。在数据中定义了要轮播的每个项目的图像和标题。在mounted钩子函数中,计算出需要创建的行数,并将它们添加到rows数组中。在模板中,使用v-for指令渲染出每一行和其中的轮播项目。使用CSS样式控制轮播项目的外观。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)