基于vue實(shí)現(xiàn)swipe分頁(yè)組件實(shí)例
項(xiàng)目背景
圖片輪播是前端項(xiàng)目必有項(xiàng),當(dāng)前有很多效果很酷炫的輪播插件,例如 Swiper 。
但是當(dāng)我們項(xiàng)目中的圖片輪播只需要一個(gè)很簡(jiǎn)單的輪播樣式,比如這樣的
我們引用這樣一個(gè) 110k 的大插件,就大材小用了。再安利一下,swiper2.x和swiper3.x對(duì)移動(dòng)和PC端支持情況如下圖
當(dāng)當(dāng)當(dāng)當(dāng)~~~
我們今天的主角登場(chǎng)了, thebird/Swipe ,這個(gè)插件完成了圖片輪播需要的基本功能,只有 14.2k ,真真的 輕量級(jí) 啊。還有,還有
翻譯一下,就是俺們?nèi)С郑还苣闶荘C端(IE7+)還是移動(dòng)端瀏覽器。此處應(yīng)該有掌聲,哈哈~
簡(jiǎn)而言之,就是當(dāng)需要一個(gè)簡(jiǎn)單的輪播時(shí),可以選用 thebird/Swipe ,自己寫(xiě)一個(gè)組件。
舉個(gè)栗子,就是我實(shí)現(xiàn)的這個(gè)—— 基于 vue 實(shí)現(xiàn)swipe分頁(yè)組件 ,移動(dòng)端和PC端均適用哦。
Result
Usage
一般情況,輪播圖片因?yàn)槭且?jīng)常換的,故在后臺(tái)定制,定制內(nèi)容如下
<div><a href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" "/></a></div> <div><a href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" "/></a></div> <div><a href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" "/></a></div>
沒(méi)有定制,必須在代碼里寫(xiě)的話,也是可以的,造一個(gè)data數(shù)組swipeInfo
<!--js--> data:{ swipeInfo:[{ href:"http://www.baidu.com", imgSrc:"" },{ href:"http://www.baidu.com", imgSrc:"" },{ href:"http://www.baidu.com", imgSrc:"" }] }, components: { 'swipe-module': require('pagination-swipe'), },
在html中綁定該數(shù)據(jù)
<!--html--> <swipe-module :swipeinfo="swipeInfo"></swipe-module>
pagination-swipe組件內(nèi)容
按照swipe構(gòu)造html框架,添加了pagination塊
<!--template.html--> <div v-el:swipe class='swipe bar-slider'> <div class='swipe-wrap'> <div v-for="item in swipeinfo"><a :href=item.href><img :src=item.imgSrc /></a></div> </div> <!-- 分頁(yè) --> <div class="pagination"> <span class="swipe-pagination-switch swipe-active-switch" @click="slideToCur(0)"></span> <span class="swipe-pagination-switch" @click="slideToCur($index+1)" v-for="item in slideNum"></span> </div> </div>
vue構(gòu)造組件
//index.js require('./style.less'); var Swipe = require('swipe'); Vue.component('pagination-swipe',{ props: ['swipeinfo'], template: require('raw!./template.html'), data: function() { return { mySwipe: {}, slideNum: {}, }; }, ready: function() { var self = this; //獲取子組件中分頁(yè)小圈圈 var slides = self.$els.swipe.getElementsByClassName('swipe-pagination-switch'); self.mySwipe = new Swipe(self.$els.swipe, { startSlide: 0, continuous: true, speed: 1000, auto: 4000, stopPropagation: false, callback: function(index, elem) { //渲染分頁(yè)小圈圈 for (var i = 0; i < slides.length; i++) { if (i != index) { slides[i].style.opacity = "0.2"; slides[i].style.background = "#000"; } else { slides[index].style.opacity = "1"; slides[index].style.background = "#ee3a4a"; } } }, }); self.slideNum = self.mySwipe.getNumSlides() - 1; }, methods: { //點(diǎn)擊底部小圈圈,跳到其所對(duì)應(yīng)頁(yè) slideToCur: function(index) { var self = this; self.mySwipe.slide(index, 300); }, } });
<!--style.less--> .swipe { overflow: hidden; visibility: hidden; position: relative; height: 200/@rem; .swipe-wrap { position: relative; overflow: hidden; height: 100%; div { float: left; width: 100%; position: relative; margin: 0; a { width: 100%; height: 100%; background-position: center 0; background-repeat: no-repeat; background-color: transparent; display: block; img { width: 100%; height: 100%; } } } } .pagination { text-align: center; position: relative; bottom: 40/@rem; cursor: pointer; } .swipe-pagination-switch { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 100%; background: #000; opacity: 0.2; margin: 0 8px; z-index: 10; &:first-child { background: #ee3a4a; } } .swipe-active-switch { opacity: 1; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue2.5通過(guò)json文件讀取數(shù)據(jù)的方法
本文通過(guò)實(shí)例代碼給大家詳細(xì)介紹了Vue2.5通過(guò)json文件讀取數(shù)據(jù)的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-02-02使用Vue3實(shí)現(xiàn)一個(gè)穿梭框效果的示例代碼
這篇文章主要給大家介紹了如何使用?Vue3?實(shí)現(xiàn)一個(gè)穿梭框效果,當(dāng)選中數(shù)據(jù),并且點(diǎn)擊相對(duì)應(yīng)的方向箭頭時(shí),選中的數(shù)據(jù)會(huì)發(fā)送到對(duì)面,并且數(shù)據(jù)會(huì)保持正確的順序進(jìn)行排列,文中有詳細(xì)的代碼講解,具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12在VUE3中禁止網(wǎng)頁(yè)返回到上一頁(yè)的方法
這篇文章主要介紹了在VUE3中如何禁止網(wǎng)頁(yè)返回到上一頁(yè),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09vue前端測(cè)試開(kāi)發(fā)watch監(jiān)聽(tīng)data的數(shù)據(jù)變化
這篇文章主要為大家介紹了vue測(cè)試開(kāi)發(fā)watch監(jiān)聽(tīng)data的數(shù)據(jù)變化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Vue2/3?登錄后用戶無(wú)操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線(示例代碼)
這篇文章主要介紹了Vue2/3?登錄后用戶無(wú)操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07vue3集成echarts數(shù)據(jù)刷新后圖表不刷新的解決方法
vue3 集成 echarts 最大的坑就是出現(xiàn)了,reactive 的數(shù)據(jù) 刷新了,但圖表缺不會(huì)刷新,所以本文就給大家詳細(xì)的介紹一下vue3集成echarts數(shù)據(jù)刷新后圖表不刷新的解決方法,需要的朋友可以參考下2023-08-08vue2.0+vue-dplayer實(shí)現(xiàn)hls播放的示例
這篇文章主要介紹了vue2.0+vue-dplayer實(shí)現(xiàn)hls播放的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vue?element-ui的el-input-number默認(rèn)值設(shè)置為空方法
這篇文章主要給大家介紹了關(guān)于vue?element-ui的el-input-number默認(rèn)值設(shè)置為空的相關(guān)資料,el-input-number組件是Element?UI非常常用的一個(gè)數(shù)字輸入框組件,它提供了默認(rèn)值設(shè)置的選項(xiàng),需要的朋友可以參考下2023-08-08