vue写一个带缩略图的轮播图,点击缩略图可进行图片的切换、自动切换、手动切换
时间: 2024-05-17 12:16:00 浏览: 121
好的,以下是一个带缩略图的轮播图的 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,
interval: null
}
},
mounted() {
this.slideWidth = this.$refs.slides.offsetWidth / this.slides.length;
this.startAutoSlide();
},
methods: {
setCurrentSlide(index) {
this.currentIndex = index;
this.stopAutoSlide();
},
startAutoSlide() {
this.interval = setInterval(() => {
this.currentIndex = (this.currentIndex + 1) % this.slides.length;
}, 3000);
},
stopAutoSlide() {
clearInterval(this.interval);
this.startAutoSlide();
}
}
}
</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>
```
这个轮播图组件包括了一个轮播图和一个缩略图列表,同时实现了自动切换和手动切换的功能。你可以根据需要修改样式和内容,并且可以根据具体业务需求对自动切换的时间间隔进行调整。
阅读全文