vue添加vue-awesome-swiper輪播組件方式
添加vue-awesome-swiper輪播組件
1.vue項(xiàng)目中添加swiper組件,也是很常見的,通常在jQuery中的方法,其實(shí)并不適用于vue項(xiàng)目。vue由于自身的框架性問題不依賴于jQuery,所以vue最好是用自己本身的swiper內(nèi)置標(biāo)簽
2.進(jìn)入項(xiàng)目目錄,安裝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標(biāo)簽
<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>
并進(jìn)行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是一個(gè)組件自有屬性,如果notNextTick設(shè)置為true,組件則不會(huì)通過NextTick來實(shí)例化swiper,也就意味著你可以在第一時(shí)間獲取到swiper對(duì)象,假如你需要?jiǎng)偧虞d遍使用獲取swiper對(duì)象來做什么事,那么這個(gè)屬性一定要是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è)計(jì)了插件,那么插件的一些配置相關(guān)參數(shù),也應(yīng)該出現(xiàn)在這個(gè)對(duì)象中,如下debugger ? ? ? ?// debugger: true, ? ? ? ?// swiper callbacks ? ? ? ?// swiper的各種回調(diào)函數(shù)也可以出現(xiàn)在這個(gè)對(duì)象中,和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 ?// 如果你需要得到當(dāng)前的swiper對(duì)象來做一些事情,你可以像下面這樣定義一個(gè)方法屬性來獲取當(dāng)前的swiper對(duì)象,同時(shí)notNextTick必須為true ?computed: { ? ?swiper() { ? ? ?return this.$refs.mySwiper.swiper ? ?} ?}, ?mounted() { ? ?// you can use current swiper instance object to do something(swiper methods) ? ?// 然后你就可以使用當(dāng)前上下文內(nèi)的swiper對(duì)象去做你想做的事了 ? ?// 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不輪播問題
因?yàn)閟wiper渲染的時(shí)候數(shù)據(jù)還沒有加載完畢,所以swiper就不輪播了,加一個(gè)判斷就好
<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>
//輪播圖配置項(xiàng)
? swiperOption: { ? ? ? loop:true, ? ? ? autoplay:{ ? ? ? ? ? disableOnInteraction: false, ? ? ? ? ? delay: 2000, ? ? ? }, ? ? ? pagination: { ? ? ? ? ? el:'.swiper-pagination', ? ? ? ? ? clickable:true, ? ? ? ? ? // type:"bullets", ? ? ? ? ? ? ? ? }, ? ? ? autoplayDisableOnInteraction: false, ? },
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue利用dayjs封裝實(shí)現(xiàn)時(shí)間實(shí)時(shí)刷新
Day.js庫本身專注于簡化JavaScript日期和時(shí)間的操作,它的API設(shè)計(jì)直觀,且功能強(qiáng)大,可以方便地格式化日期、添加或減去時(shí)間間隔、比較日期等,本文主要介紹了Vue利用dayjs封裝實(shí)現(xiàn)時(shí)間實(shí)時(shí)刷新,需要的朋友可以參考下2024-07-07詳解Vue3中的watch偵聽器和watchEffect高級(jí)偵聽器
這篇文章主要介紹了Vue3中的watch偵聽器和watchEffect高級(jí)偵聽器,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08Vue全家桶實(shí)踐項(xiàng)目總結(jié)(推薦)
本篇文章主要介紹了Vue全家桶實(shí)踐項(xiàng)目總結(jié)(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Vue實(shí)現(xiàn)PopupWindow組件詳解
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)PopupWindow組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04