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

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

2.在需要使用輪播圖的組件中生成dom結(jié)構(gòu)
可以直接去swiper官網(wǎng)復(fù)制

3.watch監(jiān)聽(tīng)數(shù)據(jù),確保swiper渲染時(shí)一定有數(shù)據(jù)
$nextTick 確保 new swiper時(shí) 一定有dom結(jié)構(gòu)
($nextTick 經(jīng)常與操作dom的動(dòng)作一塊使用,$nextTick 可以確保dom已經(jīng)生成)

vue使用swiper5做一個(gè)輪播圖,帶有分頁(yè)器、左右箭頭樣式
折騰了半天,可算弄好了。記成文章,防止遺忘。
1.npm安裝
由于swiper5以上才支持pagination分頁(yè)器換顏色,所以我們安裝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>
<!-- 指示點(diǎn) -->
<div class="swiper-pagination" slot="pagination"></div>
<!-- 左右導(dǎo)航欄 -->
<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,
// 指示點(diǎn)
pagination: {
el: '.swiper-pagination',
clickable: true /* 分頁(yè)器點(diǎn)可以點(diǎn)擊切換 */
},
// 方向:橫向或者縱向vertical
direction: 'horizontal',
// 自動(dòng)播放
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分頁(yè)器顏色*/
}
.my-swiper img {
width: 100%;
height: auto;
}
</style>最后,看成品。

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式
這篇文章主要介紹了vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue3使用拖拽組件draggable.next的保姆級(jí)教程
做項(xiàng)目的時(shí)候遇到了一個(gè)需求,拖拽按鈕到指定位置,添加一個(gè)輸入框,這篇文章主要給大家介紹了關(guān)于vue3使用拖拽組件draggable.next的保姆級(jí)教程,需要的朋友可以參考下2023-06-06
VUE2.0自定義指令與v-if沖突導(dǎo)致元素屬性修改錯(cuò)位問(wèn)題及解決方法
這篇文章主要介紹了VUE2.0自定義指令與v-if沖突導(dǎo)致元素屬性修改錯(cuò)位問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
vue項(xiàng)目中的webpack-dev-sever配置方法
下面小編就為大家分享一篇vue項(xiàng)目中的webpack-dev-sever配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

