基于vue實(shí)現(xiàn)swipe輪播組件實(shí)例代碼
項(xiàng)目背景
圖片輪播是前端項(xiàng)目必有項(xiàng),當(dāng)前有很多效果很酷炫的輪播插件,例如Swiper。
但是當(dāng)項(xiàng)目中的圖片輪播只需要一個(gè)很簡單的輪播樣式,比如這樣的
我們引用這樣一個(gè)110k的大插件,就大材小用了。再安利一下,swiper2.x和swiper3.x對移動(dòng)和PC端支持情況如下圖
當(dāng)當(dāng)當(dāng)當(dāng)~~~
我們今天的主角登場了,thebird/Swipe,這個(gè)插件完成了圖片輪播需要的基本功能,只有14.2k,真真的輕量級 啊。還有,還有
翻譯一下,就是俺們?nèi)С?,不管你是PC端(IE7+)還是移動(dòng)端瀏覽器。此處應(yīng)該有掌聲,哈哈~
簡而言之,就是當(dāng)需要一個(gè)簡單的輪播時(shí),可以選用thebird/Swipe,自己寫一個(gè)組件。
舉個(gè)栗子,就是我實(shí)現(xiàn)的這個(gè)—— 基于vue實(shí)現(xiàn)swipe分頁組件,移動(dòng)端和PC端均適用哦。
Result
Usage
一般情況,輪播圖片因?yàn)槭且?jīng)常換的,故在后臺定制,定制內(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>
沒有定制,必須在代碼里寫的話,也是可以的,造一個(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> <!-- 分頁 --> <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; //獲取子組件中分頁小圈圈 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) { //渲染分頁小圈圈 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)擊底部小圈圈,跳到其所對應(yīng)頁 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; } }
相關(guān)推薦
目前基于vue有一個(gè)vue-swipe組件,親測輕量簡單易用,基本功能齊全,是做swipe輪播圖很好的選擇
但是這個(gè)有一些問題,
- 如果樣式放在scoped中,底部小圈圈就不見了~所以,這個(gè)的樣式使用需要注意樣式污染問題.
- IE9下沒有滑動(dòng)效果,主要是ie9對css3動(dòng)畫的不兼容
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決vue使用vant輪播組件swipe + flex時(shí)文字抖動(dòng)問題
- Vue實(shí)現(xiàn)圖片輪播組件思路及實(shí)例解析
- vue輪播組件實(shí)現(xiàn)$children和$parent 附帶好用的gif錄制工具
- vue移動(dòng)端輕量級的輪播組件實(shí)現(xiàn)代碼
- 基于Vue2x實(shí)現(xiàn)響應(yīng)式自適應(yīng)輪播組件插件VueSliderShow功能
- 使用Vue制作圖片輪播組件思路詳解
- 利用Vue實(shí)現(xiàn)移動(dòng)端圖片輪播組件的方法實(shí)例
- 基于vue.js輪播組件vue-awesome-swiper實(shí)現(xiàn)輪播圖
- Vue實(shí)現(xiàn)可復(fù)用輪播組件的方法
相關(guān)文章
vue-element-admin如何轉(zhuǎn)換成中文
這篇文章主要介紹了vue-element-admin如何轉(zhuǎn)換成中文問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03vuejs在解析時(shí)出現(xiàn)閃爍的原因及防止閃爍的方法
這篇文章主要介紹了vuejs在解析時(shí)出現(xiàn)閃爍的原因及防止閃爍的方法,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09vue 實(shí)現(xiàn)拖拽動(dòng)態(tài)生成組件的需求
這篇文章主要介紹了vue 如何實(shí)現(xiàn)拖拽動(dòng)態(tài)生成組件的需求,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-05-05electron-vue?項(xiàng)目添加啟動(dòng)loading動(dòng)畫的實(shí)現(xiàn)思路
electron-vue腳手架搭建的項(xiàng)目,在開發(fā)階段可能你注意不到項(xiàng)目啟動(dòng)慢的問題,但是在build?生成的exe可執(zhí)行文件,啟動(dòng)后,要反應(yīng)很久才能進(jìn)入到app.vue?中加載的頁面,體驗(yàn)性很差,本文給大家介紹electron?vue啟動(dòng)動(dòng)畫效果的實(shí)例代碼,感興趣的朋友一起看看吧2022-01-01vue如何通過id從列表頁跳轉(zhuǎn)到對應(yīng)的詳情頁
這篇文章主要介紹了vue如何通過id從列表頁跳轉(zhuǎn)到對應(yīng)的詳情頁 ,需要的朋友可以參考下2018-05-05Vue使用watch同時(shí)監(jiān)聽多個(gè)值的實(shí)現(xiàn)方法示例
這篇文章主要為大家介紹了Vue中使用watch同時(shí)監(jiān)聽多個(gè)值的實(shí)現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05$router.push()中通過path跳轉(zhuǎn)和通過name跳轉(zhuǎn)區(qū)別解析
今天在路由跳轉(zhuǎn)傳參時(shí)發(fā)現(xiàn)params傳參接收到的總是為空,才發(fā)現(xiàn)通過path和name傳參是有區(qū)別的,這篇文章主要介紹了$router.push()中通過path跳轉(zhuǎn)和通過name跳轉(zhuǎn)有什么區(qū)別,需要的朋友可以參考下2023-11-11Vue發(fā)送Formdata數(shù)據(jù)及NodeJS接收方式
這篇文章主要介紹了Vue發(fā)送Formdata數(shù)據(jù)及NodeJS接收方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06