swiper自定義分頁器的樣式
本文實(shí)例為大家分享了swiper自定義分頁器的樣式代碼,供大家參考,具體內(nèi)容如下
js主要代碼
pagination: { // 自定義分頁器的類名----必填項(xiàng) el: '.custom-pagination', // 是否可點(diǎn)擊----必填項(xiàng) clickable: true, // 分頁的類型是自定義的----必填項(xiàng) type: 'custom', // 自定義特殊類型分頁器,當(dāng)分頁器類型設(shè)置為自定義時(shí)可用。 renderCustom: function (swiper, current, total) { console.log(current);//1 2 3 4 } },
一、el
分頁器容器的css選擇器或HTML標(biāo)簽。分頁器等組件可以置于container之外,不同Swiper的組件應(yīng)該有所區(qū)分,如#pagination1,#pagination2。
二、分頁器樣式類型 可選
‘bullets' 圓點(diǎn)(默認(rèn))
‘fraction' 分式
‘progressbar' 進(jìn)度條
‘custom' 自定義
三、renderCustom()
自定義特殊類型分頁器,當(dāng)分頁器類型設(shè)置為自定義時(shí)可用。
四、slideToLoop()
在Loop模式下Swiper切換到指定slide。切換到的是slide索引是realIndex
源碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <!-- 1、CDN引入文件 --> <link rel="stylesheet" > <script src="https://unpkg.com/swiper/swiper-bundle.js"> </script> <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.2.1.min.js"></script> <!--2、文件自己引入 --> <!-- <script src="swiper.min.js"></script> <link rel="stylesheet" href="swiper.min.css" rel="external nofollow" > <script src="jquery.js"></script> --> <style> * { margin: 0; padding: 0; } ul { list-style: none; text-align: center; overflow: hidden; } a { text-decoration: none; color: #000; } .swiper-content { width: 80%; overflow: hidden; margin: 0 auto; } .swiper-content .title { height: 100px; display: flex; align-items: center; justify-content: space-around; } .swiper-content .nav-item { float: left; display: block; margin-right: 30px; width: 50px; height: 30px; line-height: 30px; } /* 點(diǎn)擊的激活樣式 */ .swiper-content .nav-item.active { background-color: #378ee6; } /* 輪播圖的樣式 */ .swiper-content .swiper-container { height: 300px; background-color: pink; } </style> </head> <body> <div class="swiper-content"> <!-- 自定義導(dǎo)航 --> <div class="title"> <!-- 給ul 添加自定義分頁器類名 custom-pagination --> <ul class="nav custom-pagination"> <li class="nav-item active"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a> </li> <li class="nav-item"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >網(wǎng)站</a> </li> <li class="nav-item"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >關(guān)于</a> </li> <li class="nav-item"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >聯(lián)系</a> </li> </ul> </div> <!-- 輪播圖 --> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"> <h1>1</h1> </div> <div class="swiper-slide"> <h1>2</h1> </div> <div class="swiper-slide"> <h1>3</h1> </div> <div class="swiper-slide"> <h1>4</h1> </div> </div> </div> </div> <script> var mySwiper = new Swiper('.swiper-container', { // 循環(huán)模式 loop: true, pagination: { // 自定義分頁器的類名----必填項(xiàng) el: '.custom-pagination', // 是否可點(diǎn)擊----必填項(xiàng) clickable: true, // 分頁的類型是自定義的----必填項(xiàng) type: 'custom', // 自定義特殊類型分頁器,當(dāng)分頁器類型設(shè)置為自定義時(shí)可用。 renderCustom: function (swiper, current, total) { // console.log(current);//1 2 3 4 // 1、分頁器激活樣式的改變---給自己添加激活樣式并將兄弟的激活樣式移出。 $('.custom-pagination').children().eq(current - 1).addClass('active').siblings().removeClass('active'); // 2、分頁器點(diǎn)擊的時(shí)候同時(shí)切換輪播圖(事件委托)----循環(huán)模式slideToLoop-- $('.custom-pagination').on('click', 'li', function () { mySwiper.slideToLoop($(this).index(), 1000, false) }) } }, }) </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript知識(shí)點(diǎn)總結(jié)(十六)之Javascript閉包(Closure)代碼詳解
閉包是可以包含自由(未綁定)變量的代碼塊;這些變量不是在這個(gè)代碼塊或者任何全局上下文中定義的,而是在定義代碼塊的環(huán)境中定義。本文主要介紹了javascript中的閉包,感興趣的朋友一起看看吧2016-05-05layui添加動(dòng)態(tài)菜單與選項(xiàng)卡
這篇文章主要為大家詳細(xì)介紹了layui添加動(dòng)態(tài)菜單與選項(xiàng)卡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07js實(shí)現(xiàn)時(shí)間軸自動(dòng)排列效果
本文主要介紹了js實(shí)現(xiàn)新增加事件:時(shí)間軸自動(dòng)排列效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03用javascript實(shí)現(xiàn)倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了用javascript實(shí)現(xiàn)倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02