vue中使用swiper,左右箭頭點(diǎn)擊沒(méi)有效果問(wèn)題及解決
vue使用swiper,左右箭頭點(diǎn)擊沒(méi)有效果
swiper作為一個(gè)開(kāi)源的前端組件,主要用來(lái)做各種頁(yè)面切換輪播的效果。
在做左右切換效果時(shí),發(fā)現(xiàn)點(diǎn)擊左右箭頭沒(méi)有效果,原來(lái)是需要在左右箭頭的頁(yè)面標(biāo)簽上添加點(diǎn)擊事件才行。
代碼如下,親測(cè)可用
<swiper ref="mySwiper" :options="swiperOptions"> <swiper-slide><div style="background-color: #5cb85c;height: 100%"><img src="../assets/logo.png" style="height: 100%;width: 100%"></div></swiper-slide> <swiper-slide><div style="background-color: #5cb85c;height: 100%"><img src="../assets/logo.png" style="height: 100%;width: 100%"></div></swiper-slide> <swiper-slide><div style="background-color: #5cb85c;height: 100%"><img src="../assets/logo.png" style="height: 100%;width: 100%"></div></swiper-slide> <swiper-slide><div style="background-color: #5cb85c;height: 100%"><img src="../assets/logo.png" style="height: 100%;width: 100%"></div></swiper-slide> <swiper-slide><div style="background-color: #5cb85c;height: 100%"><img src="../assets/logo.png" style="height: 100%;width: 100%"></div></swiper-slide> <swiper-slide><div style="background-color: #5cb85c;height: 100%"><img src="../assets/logo.png" style="height: 100%;width: 100%"></div></swiper-slide> <!-- Add Pagination --> <div class="swiper-pagination" slot="pagination"></div> </swiper> <!-- Add Arrows --> <div class="swiper-button-next" slot="button-next" @click="next"></div> <div class="swiper-button-prev" slot="button-prev" @click="prev"></div> data(){ return{ swiperOptions: { slidesPerView: 3, slidesPerGroup: 3, loopFillGroupWithBlank: true, pagination: { el: '.swiper-pagination', clickable: true, }, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, }, } }, computed:{ swiper() { return this.$refs.mySwiper.$swiper } }, methods:{ prev(){ this.swiper.slidePrev(); }, next(){ this.swiper.slideNext(); }, }
swiper插件自定義切換箭頭按鈕
不知道大家在使用swiper時(shí)有沒(méi)有遇到這樣一種需求,話不多說(shuō),直接上gif。
也就是需要把左右切換的箭頭移到容器的外面,自定義箭頭的樣式。
給swiper容器再加一個(gè)父容器,兩個(gè)容器之間留下間隙,箭頭定位到間隙之間就ok了。
箭頭默認(rèn)是絕對(duì)定位的,給父容器一個(gè)相對(duì)定位,就能夠調(diào)整箭頭位置。此外箭頭用的是背景圖,改變箭頭大小的同時(shí)記得改變背景圖大小。
上代碼。
<style> .out_container{ width: 280px; height: 150px; margin: 100px auto; position: relative; outline: 1px solid black; } .in_container{ width: 216px; height: 130px; margin: 0 auto; overflow: hidden; } .swiper_btn{ width: 20px; height: 20px; background-size: contain; } </style>
<body> <div class="out_container"> <div class="in_container"> <div class="swiper-wrapper"> <div class="swiper-slide"><img src="" alt=""></div> <div class="swiper-slide"><img src="" alt=""></div> <div class="swiper-slide"><img src="" alt=""></div> </div> <div class="swiper-button-prev swiper_btn"></div> <div class="swiper-button-next swiper_btn"></div> </div> </div> </body>
<script> var mySwiper = new Swiper('.in_container', { prevButton: '.swiper-button-prev', nextButton: '.swiper-button-next', }) </script>
效果如下
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue 判斷兩個(gè)時(shí)間插件結(jié)束時(shí)間必選大于開(kāi)始時(shí)間的代碼
這篇文章主要介紹了vue 判斷兩個(gè)時(shí)間插件結(jié)束時(shí)間必選大于開(kāi)始時(shí)間的代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11VUE前后端學(xué)習(xí)tab寫(xiě)法實(shí)例
在本篇文章里小編給大家分享了關(guān)于VUE前后端學(xué)習(xí)tab寫(xiě)法實(shí)例以及相關(guān)知識(shí)點(diǎn),需要的朋友們參考下。2019-08-08vue使用rem實(shí)現(xiàn) 移動(dòng)端屏幕適配
這篇文章主要介紹了vue使用rem實(shí)現(xiàn) 移動(dòng)端屏幕適配的相關(guān)知識(shí),通過(guò)實(shí)例代碼介紹了vue用rem布局的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-09-09vue-router傳遞參數(shù)的幾種方式實(shí)例詳解
vue-router傳遞參數(shù)分為兩大類,一類是編程式的導(dǎo)航 router.push另一類是聲明式的導(dǎo)航 <router-link>,本文通過(guò)實(shí)例代碼給大家介紹vue-router傳遞參數(shù)的幾種方式,感興趣的朋友跟隨小編一起看看吧2018-11-11使用Vue自定義數(shù)字鍵盤(pán)組件(體驗(yàn)度極好)
最近做 Vue 開(kāi)發(fā),因?yàn)橛胁簧夙?yè)面涉及到金額輸入,產(chǎn)品老是覺(jué)得用原生的 input 進(jìn)行金額輸入的話 體驗(yàn)很不好,于是自己動(dòng)手寫(xiě)了一個(gè)使用Vue自定義數(shù)字鍵盤(pán)組件,具體實(shí)現(xiàn)代碼大家參考下本文2017-12-12elementui源碼學(xué)習(xí)仿寫(xiě)一個(gè)el-tooltip示例
本篇文章記錄仿寫(xiě)一個(gè)el-tooltip組件細(xì)節(jié),從而有助于大家更好理解餓了么ui對(duì)應(yīng)組件具體工作細(xì)節(jié),本文是elementui源碼學(xué)習(xí)仿寫(xiě)系列的又一篇文章,后續(xù)空閑了會(huì)不斷更新并仿寫(xiě)其他組件2023-07-07對(duì)vue中的input輸入框進(jìn)行郵箱驗(yàn)證方式
這篇文章主要介紹了對(duì)vue中的input輸入框進(jìn)行郵箱驗(yàn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue使用JSEncrypt對(duì)密碼本地存儲(chǔ)時(shí)加解密的實(shí)現(xiàn)
本文主要介紹了vue使用JSEncrypt對(duì)密碼本地存儲(chǔ)時(shí)加解密,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07