vue仿element實(shí)現(xiàn)分頁(yè)器效果
1 .起因
今日看完element中分頁(yè)器的源碼實(shí)現(xiàn),比較簡(jiǎn)單,遂自己按著理解實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的分頁(yè)器,記錄下來(lái),以便日后溫習(xí).
2.實(shí)現(xiàn)難點(diǎn)
分頁(yè)器的實(shí)現(xiàn)難點(diǎn)主要是什么時(shí)候顯示分頁(yè)器的省略, 我的思路是: 規(guī)定一個(gè)值foldPage, 意為當(dāng)前最多顯示的標(biāo)簽數(shù),當(dāng)總頁(yè)數(shù)超過(guò)即顯示省略.省略分為左邊省略(folder1)和右邊省略(folder2),布局代碼如下:
<div class="pagination" @click="pageClick">
<button class="pre">上一頁(yè)</button>
<ul class="pages">
<li :class="['first', { 'active' : current == 1 }]" v-if="total">
1
</li>
<li :class="[ testLeft,goback]"
v-show="showPreMore"
@mouseenter="testLeft='more-left'"
@mouseleave="testLeft='more'"></li>
<li :class="['page-item', { 'active' : current == item }]" v-for="item in $pages">
{{ item }}
</li>
<li :class="[ testRight,gogo]"
v-show="showNextMore"
@mouseenter="testRight='more-right'"
@mouseleave="testRight='more'"></li>
<li :class="['last', { 'active' : current == $last }]" v-if="total">
{{ $last }}
</li>
</ul>
<button class="next">下一頁(yè)</button>
</div>
$pages是一個(gè)計(jì)算屬性,用于動(dòng)態(tài)生成中間的頁(yè)碼,以及控制folder1和folder2的顯示,代碼如下:
computed:{
// 中間頁(yè)數(shù)組
$pages(){
const foldPage = this.foldPage
const current = Number(this.current)
const halfFoldPage = Math.floor((foldPage-2)/2)
if (this.$last > foldPage){
if (current - halfFoldPage > 2){
this.showPreMore = true
}else {
this.showPreMore = false
}
if (current + halfFoldPage < this.$last){
this.showNextMore = true
}else {
this.showNextMore = false
}
}
let array = []
// folder1顯示
if (this.showNextMore && !this.showPreMore){
for(let i = 2; i < foldPage; i++){
array.push(i)
}
// folder1 和 folder2都顯示
}else if ( this.showPreMore && this.showNextMore ){
for(let i = current - halfFoldPage; i <= current + halfFoldPage; i++ ){
array.push(i)
}
// folder2顯示
}else if (!this.showNextMore && this.showPreMore){
// 當(dāng)folder2顯示的時(shí)候,頁(yè)碼不能大于$last,需要往前多顯示差額
let dis = current + halfFoldPage - this.$last + 1;
for(let i = current - halfFoldPage - dis ; i < this.$last; i++){
array.push(i)
}
// 都不顯示
}else {
for(let i = 2; i < this.$last; i++){
array.push(i)
}
}
return array
},
// 總頁(yè)數(shù)
$last(){
return Math.ceil(this.total/this.size)
}
}
所有的點(diǎn)擊都用一個(gè)函數(shù)處理, 根據(jù)e.target判斷點(diǎn)擊的目標(biāo).從而做出相應(yīng)的邏輯:
methods:{
pageClick(e){
let newPage = Number(e.target.textContent)
this.current = Number(this.current);
if (!isNaN(newPage) && newPage){
this.current = newPage
}else {
// 下一頁(yè)
if (e.target.className.indexOf('next') != -1){
if (this.current == this.$last){
return;
}
this.current ++
}
// 上一頁(yè)
else if (e.target.className.indexOf('pre') != -1){
if (this.current == 1){
return
}
this.current --
}
// 省略向左
else if (e.target.className.indexOf('left') != -1){
this.current -= this.foldPage - 2
if (this.current <= 1){
this.current = 1
return
}
}
// 省略向右
else if(e.target.className.indexOf('right') != -1){
this.current += this.foldPage - 2
if (this.current >= this.$last){
this.current = this.$last
return
}
}
}
}
},
3.總結(jié)
pagination組件在element中算是一個(gè)很簡(jiǎn)單的組件,靜下心來(lái)看不是很復(fù)雜,理解其思路以后可以自己嘗試去寫(xiě)出來(lái),細(xì)節(jié)可以無(wú)需在意.
總結(jié)
以上所述是小編給大家介紹的vue仿element實(shí)現(xiàn)分頁(yè)器效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
基于Vue2的獨(dú)立構(gòu)建與運(yùn)行時(shí)構(gòu)建的差別(詳解)
下面小編就為大家分享一篇基于Vue2的獨(dú)立構(gòu)建與運(yùn)行時(shí)構(gòu)建的差別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Vue如何實(shí)現(xiàn)pptx在線預(yù)覽
通過(guò)PPTXjs插件,實(shí)現(xiàn)PPTX文件在線預(yù)覽,需下載PPTXjs,將其引入HTML頁(yè)面,并編寫(xiě)相應(yīng)的HTML和JS代碼,如果是移動(dòng)端還需調(diào)整div大小,這是一種便捷的前端PPTX轉(zhuǎn)HTML技術(shù),適合網(wǎng)頁(yè)展示使用2024-09-09
Vue3.3?+?TS4構(gòu)建實(shí)現(xiàn)ElementPlus功能的組件庫(kù)示例
Vue.js?是目前最盛行的前端框架之一,而?TypeScript?則是一種靜態(tài)類型言語(yǔ),它能夠讓開(kāi)發(fā)人員在編寫(xiě)代碼時(shí)愈加平安和高效,本文將引見(jiàn)如何運(yùn)用?Vue.js?3.3?和?TypeScript?4?構(gòu)建一個(gè)自主打造媲美?ElementPlus?的組件庫(kù)2023-10-10
通過(guò)實(shí)例解析chrome如何在mac環(huán)境中安裝vue-devtools插件
這篇文章主要介紹了通過(guò)實(shí)例解析chrome如何在mac環(huán)境中安裝vue-devtools插件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Vue echarts模擬后端數(shù)據(jù)流程詳解
在平常的項(xiàng)目中,echarts圖表我們也是使用的非常多的,通常我們從后端拿到數(shù)據(jù),需要在圖表上動(dòng)態(tài)的進(jìn)行呈現(xiàn),接下來(lái)我們就模擬從后端獲取數(shù)據(jù)然后進(jìn)行呈現(xiàn)的方法2022-09-09
vue 做移動(dòng)端微信公眾號(hào)采坑經(jīng)驗(yàn)記錄
這篇文章主要介紹了vue 做移動(dòng)端微信公眾號(hào)采坑經(jīng)驗(yàn)記錄,文中是小編記錄的三個(gè)坑及解決方案,需要的朋友可以參考下2018-04-04
el-popover如何通過(guò)js手動(dòng)控制彈出框顯示、隱藏
最近項(xiàng)目中多次用到了Popover彈出框,下面這篇文章主要給大家介紹了關(guān)于el-popover如何通過(guò)js手動(dòng)控制彈出框顯示、隱藏的相關(guān)資料,需要的朋友可以參考下2023-12-12

