vue中使用swiper5方式
vue使用swiper5
官網使用方法 詳情 :Swiper使用方法 - Swiper中文網
在vue中使用 首先 npm install --save swiper@5 // 安裝5.xx版本的swiper
1. 引入css js 文件
由于在多個組件中使用 所以直接在路口文件中引入css文件

在需要使用輪播圖的組件中引入js文件

2.在需要使用輪播圖的組件中生成dom結構
可以直接去swiper官網復制

3.watch監(jiān)聽數據,確保swiper渲染時一定有數據
$nextTick 確保 new swiper時 一定有dom結構
($nextTick 經常與操作dom的動作一塊使用,$nextTick 可以確保dom已經生成)

vue使用swiper5做一個輪播圖,帶有分頁器、左右箭頭樣式
折騰了半天,可算弄好了。記成文章,防止遺忘。
1.npm安裝
由于swiper5以上才支持pagination分頁器換顏色,所以我們安裝swiper5以及vue-awesome-swiper
npm install swiper@5.4.4 vue-awesome-swiper --save
2.所有代碼
<template>
<div>
<div>
<swiper :options="swiperOptions" class="my-swiper">
<swiper-slide> <img src="../assets/img/bg_1.jpg" /> </swiper-slide>
<swiper-slide> <img src="../assets/img/bg_2.jpg" /> </swiper-slide>
<swiper-slide> <img src="../assets/img/bg_3.jpg" /> </swiper-slide>
<!-- 指示點 -->
<div class="swiper-pagination" slot="pagination"></div>
<!-- 左右導航欄 -->
<div class="swiper-button-prev" slot="button-prev"></div>
<div class="swiper-button-next" slot="button-next"></div>
</swiper>
</div>
</div>
</template>
<script>
import {Swiper, SwiperSlide} from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
name: 'UserCenter',
components: {
Swiper,
SwiperSlide
},
data () {
return {
swiperOptions: {
// 循環(huán)
loop: true,
// 指示點
pagination: {
el: '.swiper-pagination',
clickable: true /* 分頁器點可以點擊切換 */
},
// 方向:橫向或者縱向vertical
direction: 'horizontal',
// 自動播放
autoplay: {
delay: 5000,
disableOnInteraction: false
},
// 切換速度
speed: 600,
// 左右箭頭按鈕
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
}
}
}
},
computed: {},
mounted () {},
methods: {}
}
</script>
<style scoped>
.my-swiper{
width: 100%;
height: auto;
--swiper-navigation-color: white; /*左右箭頭按鈕顏色*/
--swiper-pagination-color: white; /*pagination分頁器顏色*/
}
.my-swiper img {
width: 100%;
height: auto;
}
</style>最后,看成品。

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue+elementUI用戶修改密碼的前端驗證規(guī)則
用戶登錄后修改密碼,密碼需要一定的驗證規(guī)則,這篇文章主要介紹了vue+elementUI用戶修改密碼的前端驗證,需要的朋友可以參考下2024-03-03
詳解element上傳組件before-remove鉤子問題解決
這篇文章主要介紹了詳解element上傳組件before-remove鉤子問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
vue中v-for循環(huán)數組,在方法中splice刪除數組元素踩坑記錄
這篇文章主要介紹了vue中v-for循環(huán)數組,在方法中splice刪除數組元素踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
vue-cli-service build 環(huán)境設置方式
這篇文章主要介紹了vue-cli-service build 環(huán)境設置方式,具有很好的參考價值,希望對大家有所幫助。2023-01-01

