這篇文章將為大家詳細講解有關使用swiper怎么自定義一個分頁器,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

我們提供的服務有:成都網站設計、網站建設、外貿網站建設、微信公眾號開發(fā)、網站優(yōu)化、網站認證、嘉興ssl等。為1000+企事業(yè)單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的嘉興網站制作公司
解決問題:不想使用swiper的自帶的圓鈕式的分頁器,想使用自定義的分頁器。
解決方案:利用swiper提供的paginationCustomRender()方法(自定義特殊類型分頁器,當分頁器類型設置為自定義時可用。)
下面的代碼可以直接賦值粘貼到html文件里面然后作為項目在瀏覽器打開,但是圖片需要你引用自己的本地圖片并設置好路徑,否則你是看不到輪播圖片的。代碼如下(參考注釋很重要):
<!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;
}
/*自定義分頁器激活時的樣式表現*/
.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',//這里分頁器類型必須設置為custom,即采用用戶自定義配置
//下面方法可以生成我們自定義的分頁器到頁面上
paginationCustomRender: function(swiper, current, total) {
var customPaginationHtml = "";
for(var i = 0; i < total; i++) {
//判斷哪個分頁器此刻應該被激活
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>關于使用swiper怎么自定義一個分頁器就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當前名稱:使用swiper怎么自定義一個分頁器
URL鏈接:http://chinadenli.net/article0/ihjjoo.html
成都網站建設公司_創(chuàng)新互聯,為您提供全網營銷推廣、虛擬主機、用戶體驗、網站設計公司、網站排名、外貿網站建設
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯