vue写一个带缩略图的轮播图
时间: 2024-01-20 15:03:40 浏览: 81
vue.js实现简单轮播图效果
5星 · 资源好评率100%
好的,以下是一个简单的带缩略图的轮播图的 Vue 组件示例:
```vue
<template>
<div class="carousel">
<div class="carousel-container">
<div class="carousel-slides" :style="{ transform: 'translateX(' + (-currentIndex * slideWidth) + 'px)' }">
<div class="carousel-slide" v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" :alt="slide.title">
<h3>{{ slide.title }}</h3>
</div>
</div>
</div>
<div class="carousel-thumbnails">
<div class="carousel-thumbnail" v-for="(slide, index) in slides" :key="index" :class="{ active: index === currentIndex }" @click="setCurrentSlide(index)">
<img :src="slide.thumbnail" :alt="slide.title">
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
slides: [
{
title: 'Slide 1',
image: 'https://via.placeholder.com/500x300',
thumbnail: 'https://via.placeholder.com/100x60'
},
{
title: 'Slide 2',
image: 'https://via.placeholder.com/500x300',
thumbnail: 'https://via.placeholder.com/100x60'
},
{
title: 'Slide 3',
image: 'https://via.placeholder.com/500x300',
thumbnail: 'https://via.placeholder.com/100x60'
}
],
currentIndex: 0,
slideWidth: 0
}
},
mounted() {
this.slideWidth = this.$refs.slides.offsetWidth / this.slides.length;
},
methods: {
setCurrentSlide(index) {
this.currentIndex = index;
}
}
}
</script>
<style>
.carousel {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.carousel-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
}
.carousel-slides {
display: flex;
transition: transform 0.5s ease-in-out;
}
.carousel-slide {
position: relative;
width: 100%;
height: 100%;
flex-shrink: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.carousel-slide img {
max-width: 100%;
max-height: 100%;
}
.carousel-slide h3 {
margin-top: 10px;
}
.carousel-thumbnails {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
}
.carousel-thumbnail {
width: 100px;
height: 60px;
margin: 10px;
cursor: pointer;
transition: opacity 0.5s ease-in-out;
opacity: 0.5;
}
.carousel-thumbnail.active {
opacity: 1;
}
</style>
```
这个轮播图组件包括了一个轮播图和一个缩略图列表。你可以根据需要修改样式和内容。
阅读全文