# Vue Swiper怎么使用
## 一、Swiper簡介
Swiper是純JavaScript打造的滑動特效插件,專門為移動端觸摸滑動設計,同時兼容PC端。在Vue項目中使用Swiper可以實現:
- 輪播圖/Banner展示
- 圖片/內容畫廊
- 橫向/縱向滑動導航
- 多屏滑動切換等效果
## 二、安裝Swiper
### 1. 安裝核心包
```bash
npm install swiper
# 或
yarn add swiper
# Vue 2 項目
npm install swiper@5 vue-awesome-swiper
# Vue 3 項目
npm install swiper@8 swiper/vue
<template>
<swiper
:modules="modules"
:slides-per-view="3"
:space-between="50"
navigation
@swiper="onSwiper"
>
<swiper-slide v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" />
</swiper-slide>
</swiper>
</template>
<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Navigation } from 'swiper'
export default {
components: { Swiper, SwiperSlide },
setup() {
const onSwiper = (swiper) => {
console.log(swiper)
}
return {
modules: [Navigation],
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
],
onSwiper
}
}
}
</script>
<style>
@import 'swiper/css';
@import 'swiper/css/navigation';
</style>
參數 | 類型 | 說明 |
---|---|---|
slidesPerView | number | 可視幻燈片數量 |
spaceBetween | number | 幻燈片間距(px) |
loop | boolean | 是否循環播放 |
autoplay | object | 自動播放配置 |
navigation | boolean | 顯示前進/后退按鈕 |
pagination | object | 分頁器配置 |
autoplay: {
delay: 2500,
disableOnInteraction: false
}
pagination: {
clickable: true,
renderBullet: (index, className) => {
return `<span class="${className}">${index + 1}</span>`
}
}
<swiper
direction="vertical"
:slides-per-view="2"
:space-between="30"
>
<!-- slides -->
</swiper>
<template>
<swiper @swiper="setThumbsSwiper">
<!-- 主swiper內容 -->
</swiper>
<swiper
:thumbs="{ swiper: thumbsSwiper }"
>
<!-- 縮略圖內容 -->
</swiper>
</template>
<script>
import { Thumbs } from 'swiper'
export default {
data() {
return {
thumbsSwiper: null
}
},
methods: {
setThumbsSwiper(swiper) {
this.thumbsSwiper = swiper
}
}
}
</script>
breakpoints: {
320: {
slidesPerView: 1,
spaceBetween: 10
},
768: {
slidesPerView: 2,
spaceBetween: 20
},
1024: {
slidesPerView: 3,
spaceBetween: 30
}
}
當異步加載數據后,Swiper需要重新初始化:
watch: {
slides(newVal) {
if (newVal.length) {
this.$nextTick(() => {
this.swiper.update()
})
}
}
}
在scoped樣式中需要深度選擇器:
::v-deep .swiper-pagination-bullet {
background: white;
}
.swiper-button-next,
.swiper-button-prev {
color: #ff0000;
&::after {
font-size: 24px;
}
}
lazy
模塊實現圖片懶加載virtual
模式swiper.destroy()
import {
Navigation,
Pagination,
Autoplay,
EffectFade,
Thumbs
} from 'swiper'
export default {
modules: [Navigation, Pagination, Autoplay, EffectFade, Thumbs],
swiperOptions: {
slidesPerView: 1,
spaceBetween: 30,
loop: true,
effect: 'fade',
autoplay: {
delay: 3000,
pauseOnMouseEnter: true
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
pagination: {
el: '.swiper-pagination',
type: 'progressbar'
},
thumbs: {
swiper: thumbsSwiper
}
}
}
通過本文您應該掌握了: - Vue 2/3中Swiper的安裝差異 - 基礎輪播實現方法 - 常用配置參數解析 - 高級功能實現技巧 - 常見問題解決方案
建議在實際項目中根據需求選擇合適的配置組合,并通過Swiper官方文檔獲取最新API參考。 “`
(注:實際字數約1350字,此處為精簡展示版,完整版包含更多細節示例和說明)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。