一個swiper 兩個分頁器的寫法【總結】

寫項目的時候,使用的是swiper插件呈現的效果是一個swiper要實現兩個分頁器,下面就來總結一下

以swiper3為例來寫,在頁面中引入jquery、swiper.min.js和swiper.min.css文件。

HTML結構:

?

  <div class="banner swiper-container"><div class="swiper-wrapper"><div class="swiper-slide blue-slide">slider1</div><div class="swiper-slide red-slide">slider2</div><div class="swiper-slide orange-slide">slider3</div></div><!-- 如果需要分頁器 --><div class="swiper-pagination"></div><!-- 自定義分頁器 --><div class="swiper-num"><span class="active"></span>/<span class="total"></span></div><!-- 如果需要導航按鈕 --><div class="swiper-button-prev"></div><div class="swiper-button-next"></div></div>

?

.swiper-num這一塊是需要自己定義的。
CSS樣式:

?

   .banner .swiper-pagination-bullets{bottom: 0;}.banner .swiper-pagination-bullets{bottom: 0;}.swiper-num {position: absolute;width: 165px;left:10%;bottom: 0;z-index: 2;}.swiper-num .active {display: inline-block;}.swiper-num span {font-size: 32px;}.swiper-num .total {display: inline-block;}.banner .swiper-slide{line-height: 500px;text-align: center;font-size: 50px;}.blue-slide {background: #4390EE;color: #fff;}.red-slide {background: #CA4040;color: #fff}.orange-slide {background: #FF8604;color: #fff;}.gray-slide {background: gray;color: #fff;}

?

js代碼:

?

var mySwiper = new Swiper ('.banner', {loop: true, // 循環模式選項// 如果需要分頁器pagination:  '.swiper-pagination',// 如果需要前進后退按鈕nextButton: '.swiper-button-next',prevButton: '.swiper-button-prev',onInit: function(swiper){//Swiper初始化了var total = "0"+swiper.bullets.length;var active =swiper.activeIndex;$(".swiper-num .active").text("0"+active);$(".swiper-num .total").text(total);
},
onSlideChangeEnd: function(swiper){var active =swiper.realIndex +1;$(".swiper-num .active").text("0"+active);
}

?

onInit 回調函數,初始化后執行。
swiper.bullets.length:是獲取分頁器swiper的分頁器個數長度。
activeIndex:當前swiper-slide的索引。
onSlideChangeEnd(swiper):回調函數,swiper從一個slide過渡到另一個slide結束時執行。
swiper.realIndex:當前活動的swiper-slide的索引,與activeIndex不同的是,在loop模式下不會將復制的塊的數量計算在內。

以swiper4為例來寫:
swiper4和swiper3的HTML結構一樣,css樣式也一樣,使用的版本是swiper4.5
JS代碼:

?

 var mySwiper = new Swiper ('.banner', {loop: true, // 循環模式選項loopedSlides:1,//如果是1的話不寫也行// 如果需要分頁器pagination: {el: '.swiper-pagination',},// 如果需要前進后退按鈕navigation: {nextEl: '.swiper-button-next',prevEl: '.swiper-button-prev',},on:{init:function(){console.log(this);var total=this.slides.length-2;console.log(total);$('.total').text('0'+total);this.emit('transitionEnd');},transitionEnd:function(){console.log(this.realIndex);var index=this.realIndex+1;$(".active").text("0"+index);}}})  

?

this.slides.length的長度為5,img的長度為3,所以減掉2,但是減去的前提是loopedSlides:1,的取值。
loopedSlides 在loop模式下使用slidesPerview:'auto',還需使用該參數設置所要用到的loop個數(一般設置大于可視slide個數2個即可)。
this.realIndex是從0開始的。所以要在此基礎上加1。

每天學習一點點,讓自己進步一點點。

?

?

轉載于:https://www.cnblogs.com/ling-nl/p/10935749.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393526.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393526.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393526.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

react路由守衛+重定向_React + Apollo:如何在重新查詢后進行重定向

react路由守衛重定向by Jun Hyuk Kim金俊赫 React Apollo&#xff1a;如何在重新查詢后進行重定向 (React Apollo: How to Redirect after Refetching a Query) GraphQL is hot, and for a good reason. In short, it is a query language that allows you to ask for exact…

python 爬蟲可視化編程_Python爬蟲爬取博客實現可視化過程解析

源碼&#xff1a;from pyecharts import Barimport reimport requestsnum0b[]for i in range(1,11):linkhttps://www.cnblogs.com/echoDetected/default.html?pagestr(i)headers{user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko…

tp6常用命令

TP6常用命令 1.創建控制器 php think make:controller --plain index** (php think make:controller --plain 控制器名稱&#xff08;首字母大寫&#xff09;)2.創建模型 php think make:model 【模塊名】/模型名 模型名為表名相當3.創建中間件 php think make:middleware 中…

Problem B: 字符類的封裝

Description 先來個簡單習題&#xff0c;練練手吧&#xff01;現在需要你來編寫一個Character類&#xff0c;將char這一基本數據類型進行封裝。該類中需要有如下成員函數&#xff1a; 1. 無參構造函數。 2. 構造函數Character(char)&#xff1a;用參數初始化數據成員。 3. void…

leetcode852. 山脈數組的峰頂索引(二分法)

我們把符合下列屬性的數組 A 稱作山脈&#xff1a; A.length > 3 存在 0 < i < A.length - 1 使得A[0] < A[1] < … A[i-1] < A[i] > A[i1] > … > A[A.length - 1] 給定一個確定為山脈的數組&#xff0c;返回任何滿足 A[0] < A[1] < … A[i…

linux 一鍵安裝lnmp

運行下面這天命令&#xff0c;回車 wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5 && ./install.sh lnmp 選擇數據庫版本&#xff0c;回車 設置MySQL的root密碼&#xff08;為了安全不…

圖標下載

個人認為非常好的一個網站&#xff1a; http://www.easyicon.net/

以太坊ipfs_動手:Infura和以太坊上的IPFS入門

以太坊ipfsby Niharika Singh由Niharika Singh 動手&#xff1a;Infura和以太坊上的IPFS入門 (Hands On: Get Started With Infura and the IPFS on Ethereum) 為什么選擇Infura&#xff1f; (Why Infura?) There are a lot of pain points being faced by blockchain which …

suse required-start: mysql_suse linux 安裝MySql步驟

今天下午終于把mysql搞定了&#xff0c;我安裝的這個linux版本(suselinux10.0)自己帶的有Mysql&#xff0c;但是&#xff0c;在網上查的版本要比這高&#xff0c;所以就上網找了一個然后自己裝&#xff0c;但是從來沒有接觸過MySql也不知道該怎么裝&#xff0c;于是就上網找&am…

PHP上傳文件到七牛云和阿里云

七牛云上傳 注冊七牛云賬號并認證 進入控制臺找到對象存儲添加一個新的倉庫 添加完成之后看文檔 安裝 使用 Composer 安裝 Composer是 PHP 依賴管理工具。你可以在自己的項目中聲明所依賴的外部工具庫&#xff0c;Composer 會自動幫你安裝這些依賴的庫文件。 ???1. 安裝…

變態青蛙跳

2019獨角獸企業重金招聘Python工程師標準>>> 題目描述 一只青蛙一次可以跳上1級臺階&#xff0c;也可以跳上2級……它也可以跳上n級。求該青蛙跳上一個n級的臺階總共有多少種跳法。 相比普通青蛙跳&#xff0c;這個 n級的就有點難了&#xff0c;重點是 能跳n級&…

中間的數(若已經排好序)

描述&#xff1a; 奇數個&#xff0c;輸出中間那個 偶數個&#xff0c;輸出中間那倆 代碼&#xff1a; #include<iostream>using namespace std;int main(){ int *a; int n; cin>>n; anew int[n]; for(int i0; i<n; i) cin>>a[i]; …

leetcode1237. 找出給定方程的正整數解(二分法)

給出一個函數 f(x, y) 和一個目標結果 z&#xff0c;請你計算方程 f(x,y) z 所有可能的正整數 數對 x 和 y。 給定函數是嚴格單調的&#xff0c;也就是說&#xff1a; f(x, y) < f(x 1, y) f(x, y) < f(x, y 1) 函數接口定義如下&#xff1a; interface CustomFunc…

數據庫 測試數據生成_我們的測試數據生成器如何使假數據看起來真實

數據庫 測試數據生成by Tom Winter湯姆溫特(Tom Winter) 我們的測試數據生成器如何使假數據看起來真實 (How our test data generator makes fake data look real) We recently released DataFairy, a free tool that generates test data. But first, let me tell you the st…

tp框架生命周期

1、入口文件 用戶發起的請求都會經過應用的入口文件&#xff0c;通常是 public/index.php文件。當然&#xff0c;你也可以更改或者增加新的入口文件。 通常入口文件的代碼都比較簡單&#xff0c;一個普通的入口文件代碼如下&#xff1a; // 應用入口文件 // 定義項目路徑 d…

django 創建mysql失敗_創建表時出現Django MySQL錯誤

我正在用MySQL數據庫構建一個django應用程序。當我第一次運行“python manage.py migrate”時&#xff0c;一些表創建得很好&#xff0c;然后出現一些錯誤。出現的錯誤是&#xff1a;django.db.utils.IntegrityError: (1215, Cannot add foreign keyconstraint)當我運行這個MyS…

Laravel數據庫遷移和填充(支持中文)

寫在前面 經常我們做項目都團隊協作開發&#xff0c;每個人都在自己本地的數據庫&#xff0c;如果你曾經出現過讓同事手動在數據庫結構中添加字段的情況&#xff0c;數據庫遷移可以解決你這個問題。 不僅如此&#xff0c;在線上部署的時候&#xff0c;也避免了手動導入數據庫或…

leetcode374. 猜數字大小(二分法)

猜數字游戲的規則如下&#xff1a; 每輪游戲&#xff0c;系統都會從 1 到 n 隨機選擇一個數字。 請你猜選出的是哪個數字。 如果你猜錯了&#xff0c;系統會告訴你這個數字比系統選出的數字是大了還是小了。 你可以通過調用一個預先定義好的接口 guess(int num) 來獲取猜測結果…

什么情況下你的工作最為成功_如何在沒有工作經驗的情況下獲得技術工作

什么情況下你的工作最為成功by Anthony Sistilli安東尼西斯蒂里(Anthony Sistilli) 如何在沒有工作經驗的情況下獲得技術工作 (How to get a tech job with no previous work experience) I run a free community called the Forge where I help students navigate the world …

jquery批量刪除

前臺代碼 <!doctype html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport"content"widthdevice-width, user-scalableno, initial-scale1.0, maximum-scale1.0, minimum-scale1.0">…