Vue中使用Swiper簡(jiǎn)單封裝組件示例
正文
Swiper的最簡(jiǎn)單使用,參考官網(wǎng)教程
但通常情況下,<swiper-slide></swiper-slide>
作為循環(huán)展示的內(nèi)容,需要能夠展示多種樣式的循環(huán)列表,此時(shí)必須要使用slot
需要封裝的Swiper
組件命名為MySwiper.vue
,代碼如下
loop
表示是否循環(huán)展示,值為false
時(shí)會(huì)來(lái)回切換,相當(dāng)魔性autoplay
是否自動(dòng)循環(huán)展示,值為false時(shí)不會(huì)自動(dòng)循環(huán),delay
為每頁(yè)停留的時(shí)間
需要循環(huán)的部分<swiper-slide>
,其中應(yīng)當(dāng)包含的內(nèi)容就是需要循環(huán)展示的自定義組件,使用slot
插槽占位
在調(diào)用時(shí),首先從父組件獲取數(shù)據(jù)showList
,傳至子組件MySwiper
,MySwiper
中<swiper-slide>
循環(huán)若干次,也就生成了若干的slot
,在調(diào)用slot
的時(shí)候,綁定名為item的attribute,在父組件中可以通過(guò)v-slot
接受,參數(shù)起名為slotProps
,b包含所有slot中傳輸?shù)腶ttribute,此處包含傳輸?shù)膇tem。
作用域插槽
<template> <div class="swiper-display"> <swiper :modules="modules" :slides-per-view="1" :space-between="50" navigation :pagination="{ clickable: true }" :scrollbar="{ draggable: true }" @swiper="onSwiper" @slideChange="onSlideChange" loop autoplay="{ delay: 2000 }" > <swiper-slide class="swiper-item" v-for="item in list" :key="item.seq"> <slot :item="item"></slot> </swiper-slide> </swiper> </div> </template> <script> import { Navigation, Pagination, Scrollbar, A11y, Autoplay } from 'swiper'; import { Swiper, SwiperSlide } from 'swiper/vue'; import { onMounted } from 'vue'; import 'swiper/css'; export default { components: { Swiper, SwiperSlide, }, props: { list: Array }, setup(props) { const onSwiper = (swiper) => { // console.log(swiper); }; const onSlideChange = () => { // console.log('slide change'); }; return { onSwiper, onSlideChange, modules: [Navigation, Pagination, Scrollbar, A11y, Autoplay], autoPlay }; }, } </script> <style lang="scss"> .swiper-display { width: 100%; height: 100%; position: relative; .swiper { height: 100%; .swiper-wrapper { height: 100%; .swiper-item { height: 100%; } } } } </style>
使用方式如下,其中ToDisplay
表示需要利用Swiper
展示的自定義組件
<MySwiper v-slot="slotProps" :list="showList"> <ToDisplay :item="slotProps.item"></ToDisplay> </MySwiper>
此外,需要注意css的設(shè)置。<swiper-slide>
中,如果直接寫html
內(nèi)容,例如div
之流,swiper-slide
的高度可以被正常撐開(kāi)。但如果寫成一個(gè)封裝組件,swiper-slide
的高度會(huì)渲染為0,導(dǎo)致內(nèi)容顯示為空。
此處有兩個(gè)解決方案:
- 設(shè)置
swiper-slide
的高度。雖然可以解決,但通用性較差,如果能確保所有Swiper
高度相同,可以考慮。但需要注意min-height
是無(wú)效的,不會(huì)隨著填充的內(nèi)容高度增加而增加,相當(dāng)于是一個(gè)固定值 - 將父元素逐個(gè)設(shè)置為100%,雖然有點(diǎn)傻,但能較好地適應(yīng)
Swiper
中內(nèi)容高度不同的情況
.swiper-display { width: 100%; height: 100%; position: relative; .swiper { height: 100%; .swiper-wrapper { height: 100%; .swiper-item { height: 100%; } } }
以上就是Vue中使用Swiper簡(jiǎn)單封裝組件示例的詳細(xì)內(nèi)容,更多關(guān)于Vue Swiper封裝組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue控制滾動(dòng)條滑到某個(gè)位置的方法實(shí)例
當(dāng)容器有滾動(dòng)條時(shí),有時(shí)需要將滾動(dòng)條滑到某個(gè)位置,下面這篇文章主要給大家介紹了關(guān)于vue控制滾動(dòng)條滑到某個(gè)位置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Vue3中的極致防抖/節(jié)流詳解(附常見(jiàn)方式防抖/節(jié)流)
在JavaScript中函數(shù)的防抖和節(jié)流不是什么新鮮話題,這篇文章主要給大家介紹了關(guān)于Vue3中極致防抖/節(jié)流的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02vue-virtual-scroll-list虛擬組件實(shí)現(xiàn)思路詳解
這篇文章主要給大家介紹了關(guān)于vue-virtual-scroll-list虛擬組件實(shí)現(xiàn)思路的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02使用vue-cli初始化項(xiàng)目時(shí)運(yùn)行‘npm run dev’報(bào)錯(cuò)及解決
這篇文章主要介紹了使用vue-cli初始化項(xiàng)目時(shí)運(yùn)行‘npm run dev’報(bào)錯(cuò)及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Vue使用v-model收集各種表單數(shù)據(jù)、過(guò)濾器的示例詳解
這篇文章主要介紹了Vue使用v-model收集各種表單數(shù)據(jù)、過(guò)濾器的示例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08iview給radio按鈕組件加點(diǎn)擊事件的實(shí)例
下面小編就為大家?guī)?lái)一篇iview給radio按鈕組件加點(diǎn)擊事件的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09vueScroll實(shí)現(xiàn)移動(dòng)端下拉刷新、上拉加載
這篇文章主要介紹了vueScroll實(shí)現(xiàn)移動(dòng)端下拉刷新、上拉加載,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03