请用vue帮我写一个盒子里面放一组轮播图的代码
时间: 2023-08-28 07:29:37 浏览: 97
好的,下面是一个使用Vue实现轮播图的示例代码:
```
<template>
<div class="slider-container">
<div class="slider-wrapper">
<div class="slider-slide" v-for="(img, index) in images" :key="index">
<img :src="img">
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
images: [
"https://example.com/img1.jpg",
"https://example.com/img2.jpg",
"https://example.com/img3.jpg"
]
};
},
mounted() {
let currentSlide = 0;
const slides = document.querySelectorAll(".slider-slide");
const slideCount = slides.length;
setInterval(() => {
slides[currentSlide].classList.remove("active");
currentSlide = (currentSlide + 1) % slideCount;
slides[currentSlide].classList.add("active");
}, 3000);
}
};
</script>
<style>
.slider-container {
border: 1px solid #ddd;
width: 500px;
height: 300px;
overflow: hidden;
}
.slider-wrapper {
display: flex;
width: 100%;
height: 100%;
}
.slider-slide {
flex-shrink: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 0.5s ease-in-out;
}
.slider-slide.active {
opacity: 1;
}
</style>
```
这个示例代码中,我们使用了data属性来定义轮播图片的数组,然后在mounted钩子函数中使用setInterval函数实现轮播效果。同时,我们使用了CSS的flexbox布局来使轮播图居中,并且使用了opacity属性来实现渐变效果。
阅读全文