用vue写一段有轮播图的页面
时间: 2023-06-11 14:06:18 浏览: 70
基于vue的轮播图
好的,以下是一个简单的Vue页面,带有轮播图效果:
```
<template>
<div class="carousel-container">
<div class="carousel">
<div class="slide" v-for="(image, index) in images" :key="index">
<img :src="image.url" :alt="image.alt">
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
images: [
{
url: "./images/image1.jpg",
alt: "Image 1"
},
{
url: "./images/image2.jpg",
alt: "Image 2"
},
{
url: "./images/image3.jpg",
alt: "Image 3"
}
],
currentSlide: 0
}
},
mounted() {
setInterval(() => {
this.currentSlide = (this.currentSlide + 1) % this.images.length;
}, 3000);
}
}
</script>
<style>
.carousel-container {
position: relative;
height: 500px;
}
.carousel {
display: flex;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.slide {
flex: 1 0 100%;
transition: transform 0.5s ease-in-out;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.slide.active {
transform: translateX(-100%);
}
</style>
```
该页面包含了一个名为`images`的数组,其中包含了轮播图中的图片的URL和alt文本。`currentSlide`用于跟踪当前显示的幻灯片的索引。`mounted`生命周期钩子函数用于设置轮播图的计时器,每隔3秒钟切换到下一个幻灯片。CSS样式用于创建幻灯片效果。
阅读全文