vue左右滑动翻页效果
时间: 2023-08-29 12:10:56 浏览: 92
你可以使用vue-awesome-swiper插件实现左右滑动翻页效果。具体步骤如下:
1. 安装vue-awesome-swiper插件
```
npm install vue-awesome-swiper --save
```
2. 在Vue组件中引入Swiper组件
```
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-scrollbar"></div>
</div>
</template>
<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
export default {
components: {
Swiper,
SwiperSlide
},
data() {
return {
swiperOption: {
// Swiper选项
slidesPerView: 3,
spaceBetween: 30,
freeMode: true,
pagination: {
el: '.swiper-pagination'
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
scrollbar: {
el: '.swiper-scrollbar'
}
}
}
}
}
</script>
<style>
/* Swiper样式 */
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: flex;
justify-content: center;
align-items: center;
}
</style>
```
3. 在Vue组件中配置Swiper选项
```
data() {
return {
swiperOption: {
// Swiper选项
slidesPerView: 3,
spaceBetween: 30,
freeMode: true,
pagination: {
el: '.swiper-pagination'
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
scrollbar: {
el: '.swiper-scrollbar'
}
}
}
}
```
4. 在Vue组件中使用Swiper组件
```
<template>
<div class="swiper-container">
<swiper :options="swiperOption">
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-scrollbar"></div>
</div>
</template>
```
5. 此时你就可以在Vue组件中看到左右滑动翻页的效果了。
阅读全文