swiper自定義分頁器使用方法詳解
本文實例為大家分享了swiper自定義分頁器使用的具體代碼,供大家參考,具體內(nèi)容如下
解決問題:不想使用swiper的自帶的圓鈕式的分頁器,想使用自定義的分頁器。
解決方案:利用swiper提供的paginationCustomRender()方法(自定義特殊類型分頁器,當(dāng)分頁器類型設(shè)置為自定義時可用。)
下面的代碼可以直接賦值粘貼到html文件里面然后作為項目在瀏覽器打開,但是圖片需要你引用自己的本地圖片并設(shè)置好路徑,否則你是看不到輪播圖片的。代碼如下(參考注釋很重要):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>swiper自定義分頁器用法</title>
<link href="swiper-3.4.2.min.css" rel="stylesheet" />
<style>
* {
padding: 0;
margin: 0;
}
.swiper-container {
position: relative;
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
.swiper-slide img {
display: block;
width: 100%;
max-width: 100%;
}
/*包裹自定義分頁器的div的位置等CSS樣式*/
.swiper-pagination-custom {
bottom: 10px;
left: 0;
width: 100%;
}
/*自定義分頁器的樣式,這個你自己想要什么樣子自己寫*/
.swiper-pagination-customs {
width: 30px;
height: 4px;
display: inline-block;
background: #000;
opacity: .3;
margin: 0 5px;
}
/*自定義分頁器激活時的樣式表現(xiàn)*/
.swiper-pagination-customs-active {
opacity: 1;
background-color: #F78E00;
}
</style>
</head>
<body>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<img src="banner1_.jpg" alt="輪播圖1" />
</div>
<div class="swiper-slide">
<img src="banner2_.jpg" alt="輪播圖2" />
</div>
</div>
<div class="swiper-pagination"></div>
</div>
</body>
<script src="jquery.min.js"></script>
<script type="text/javascript" src="swiper.min.js"></script>
<script>
var mySwiper = new Swiper('.swiper-container', {
direction: 'horizontal',
loop: true,
autoplay: 3000,//自動輪播
speed: 600,
// 如果需要分頁器
pagination: '.swiper-pagination',
paginationType: 'custom',//這里分頁器類型必須設(shè)置為custom,即采用用戶自定義配置
//下面方法可以生成我們自定義的分頁器到頁面上
paginationCustomRender: function(swiper, current, total) {
var customPaginationHtml = "";
for(var i = 0; i < total; i++) {
//判斷哪個分頁器此刻應(yīng)該被激活
if(i == (current - 1)) {
customPaginationHtml += '<span class="swiper-pagination-customs swiper-pagination-customs-active"></span>';
} else {
customPaginationHtml += '<span class="swiper-pagination-customs"></span>';
}
}
return customPaginationHtml;
}
});
</script>
</html>
代碼效果圖如下(上傳圖片大小有限制,所以我滑的有點快):

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js實現(xiàn)for循環(huán)跳過undefined值示例
這篇文章主要介紹了js實現(xiàn)for循環(huán)跳過undefined值,結(jié)合實例形式分析了js使用for循環(huán)針對數(shù)組的遍歷、判斷、運算等相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
基于js與flash實現(xiàn)的網(wǎng)站flv視頻播放插件代碼
這篇文章主要介紹了基于js與flash實現(xiàn)的網(wǎng)站flv視頻播放插件代碼,該功能在很多網(wǎng)站上都有著廣泛的應(yīng)用,本文以實例形式對其進行介紹,需要的朋友可以參考下2014-10-10
用XMLDOM和ADODB.Stream實現(xiàn)base64編碼解碼實現(xiàn)代碼
用 XMLDOM 和 ADODB.Stream 實現(xiàn)base64編碼解碼實現(xiàn)代碼,需要的朋友可以參考下。2010-11-11
微信小程序?qū)崿F(xiàn)action-sheet彈出底部菜單功能【附源碼下載】
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)action-sheet彈出底部菜單功能,結(jié)合實例形式分析了action-sheet組件彈出菜單的使用技巧,包括元素遍歷、事件響應(yīng)及屬性設(shè)置等操作方法,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12

