vue添加vue-awesome-swiper輪播組件方式
更新時間:2022年10月21日 09:27:41 作者:騎上我心愛的小摩托
這篇文章主要介紹了vue添加vue-awesome-swiper輪播組件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
添加vue-awesome-swiper輪播組件
1.vue項目中添加swiper組件,也是很常見的,通常在jQuery中的方法,其實并不適用于vue項目。vue由于自身的框架性問題不依賴于jQuery,所以vue最好是用自己本身的swiper內(nèi)置標簽
2.進入項目目錄,安裝swiper
npm install vue-awesome-swiper --save
3.在main.js中定義該swiper組件
import Vue from 'vue' //掛載swiper import VueAwesomeSwiper from 'vue-awesome-swiper'; Vue.use(VueAwesomeSwiper);
4.在代碼中插入該swiper標簽
<swiper :options="swiperOption" ref="mySwiper"> ?<!-- slides --> ?<swiper-slide>I'm Slide 1</swiper-slide> ?<swiper-slide>I'm Slide 2</swiper-slide> ?<swiper-slide>I'm Slide 3</swiper-slide> ?<swiper-slide>I'm Slide 4</swiper-slide> ?<swiper-slide>I'm Slide 5</swiper-slide> ?<swiper-slide>I'm Slide 6</swiper-slide> ?<swiper-slide>I'm Slide 7</swiper-slide> ?<!-- Optional controls --> ?<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> ?<div class="swiper-scrollbar" ? slot="scrollbar"></div> </swiper>
并進行swiper的配置
import { swiper, swiperSlide } from 'vue-awesome-swiper'
數(shù)據(jù)方法配置
export default { ? name: '', ?data() { ? ?return { ? ? ?swiperOption: { ? ? ? ?// NotNextTick is a component's own property, and if notNextTick is set to true, the component will not instantiate the swiper through NextTick, which means you can get the swiper object the first time (if you need to use the get swiper object to do what Things, then this property must be true) ? ? ? ?// notNextTick是一個組件自有屬性,如果notNextTick設(shè)置為true,組件則不會通過NextTick來實例化swiper,也就意味著你可以在第一時間獲取到swiper對象,假如你需要剛加載遍使用獲取swiper對象來做什么事,那么這個屬性一定要是true ? ? ? ?notNextTick: true, ? ? ? ?// swiper configs 所有的配置同swiper官方api配置 ? ? ? ?autoplay: 3000, ? ? ? ?// direction : 'vertical', ? ? ? ?effect:"coverflow", ? ? ? ?grabCursor : true, ? ? ? ?setWrapperSize :true, ? ? ? ?// autoHeight: true, ? ? ? ?// paginationType:"bullets", ? ? ? ?pagination : '.swiper-pagination', ? ? ? ?paginationClickable :true, ? ? ? ?prevButton:'.swiper-button-prev', ? ? ? ?nextButton:'.swiper-button-next', ? ? ? ?// scrollbar:'.swiper-scrollbar', ? ? ? ?mousewheelControl : true, ? ? ? ?observeParents:true, ? ? ? ?// if you need use plugins in the swiper, you can config in here like this ? ? ? ?// 如果自行設(shè)計了插件,那么插件的一些配置相關(guān)參數(shù),也應(yīng)該出現(xiàn)在這個對象中,如下debugger ? ? ? ?// debugger: true, ? ? ? ?// swiper callbacks ? ? ? ?// swiper的各種回調(diào)函數(shù)也可以出現(xiàn)在這個對象中,和swiper官方一樣 ? ? ? ?// onTransitionStart(swiper){ ? ? ? ?// ? console.log(swiper) ? ? ? ?// }, ? ? ? ?// more Swiper configs and callbacks... ? ? ? ?// ... ? ? ?} ? ?} ?},components: { ?swiper, ?swiperSlide }, ?// you can find current swiper instance object like this, while the notNextTick property value must be true ?// 如果你需要得到當前的swiper對象來做一些事情,你可以像下面這樣定義一個方法屬性來獲取當前的swiper對象,同時notNextTick必須為true ?computed: { ? ?swiper() { ? ? ?return this.$refs.mySwiper.swiper ? ?} ?}, ?mounted() { ? ?// you can use current swiper instance object to do something(swiper methods) ? ?// 然后你就可以使用當前上下文內(nèi)的swiper對象去做你想做的事了 ? ?// console.log('this is current swiper instance object', this.swiper) ? ?// this.swiper.slideTo(3, 1000, false) ?} }
5.最后引入swiper樣式
@import'../src/style/swiper.min.css';
vue-awesome-swiper不輪播問題
因為swiper渲染的時候數(shù)據(jù)還沒有加載完畢,所以swiper就不輪播了,加一個判斷就好
<div class="banner-wrap" ? v-if='bannerList.length'>? ? ? ? ?? ? ? ? ? <swiper :options="swiperOption" ref="mySwiper" > ? ? ? ? ? ? <swiper-slide v-for='(item,index) in bannerList' :key = 'index'> ? ? ? ? ? ? ? ? <div class="img-box"> ? ? ? ? ? ? ? ? ? <img :src="item.banner" alt=""> ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? </swiper-slide>? ? ? ? ? ? ? <div class="swiper-pagination" ?slot="pagination"></div> ? ? ? ? </swiper> ? ? ? </div>
//輪播圖配置項
? swiperOption: { ? ? ? loop:true, ? ? ? autoplay:{ ? ? ? ? ? disableOnInteraction: false, ? ? ? ? ? delay: 2000, ? ? ? }, ? ? ? pagination: { ? ? ? ? ? el:'.swiper-pagination', ? ? ? ? ? clickable:true, ? ? ? ? ? // type:"bullets", ? ? ? ? ? ? ? ? }, ? ? ? autoplayDisableOnInteraction: false, ? },
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Vue3中的watch偵聽器和watchEffect高級偵聽器
這篇文章主要介紹了Vue3中的watch偵聽器和watchEffect高級偵聽器,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08