vue仿element實(shí)現(xiàn)分頁器效果
1 .起因
今日看完element中分頁器的源碼實(shí)現(xiàn),比較簡單,遂自己按著理解實(shí)現(xiàn)了一個(gè)簡單的分頁器,記錄下來,以便日后溫習(xí).
2.實(shí)現(xiàn)難點(diǎn)
分頁器的實(shí)現(xiàn)難點(diǎn)主要是什么時(shí)候顯示分頁器的省略, 我的思路是: 規(guī)定一個(gè)值foldPage, 意為當(dāng)前最多顯示的標(biāo)簽數(shù),當(dāng)總頁數(shù)超過即顯示省略.省略分為左邊省略(folder1)和右邊省略(folder2),布局代碼如下:
<div class="pagination" @click="pageClick"> <button class="pre">上一頁</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">下一頁</button> </div>
$pages是一個(gè)計(jì)算屬性,用于動(dòng)態(tài)生成中間的頁碼,以及控制folder1和folder2的顯示,代碼如下:
computed:{ // 中間頁數(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í)候,頁碼不能大于$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 }, // 總頁數(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 { // 下一頁 if (e.target.className.indexOf('next') != -1){ if (this.current == this.$last){ return; } this.current ++ } // 上一頁 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è)很簡單的組件,靜下心來看不是很復(fù)雜,理解其思路以后可以自己嘗試去寫出來,細(xì)節(jié)可以無需在意.
總結(jié)
以上所述是小編給大家介紹的vue仿element實(shí)現(xiàn)分頁器效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Element InputNumber 計(jì)數(shù)器的實(shí)現(xiàn)示例
這篇文章主要介紹了Element InputNumber 計(jì)數(shù)器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08關(guān)于@click.native中?.native?的含義與使用方式
這篇文章主要介紹了關(guān)于@click.native中?.native?的含義與使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10使用el-form之表單校驗(yàn)自動(dòng)定位到報(bào)錯(cuò)位置問題
這篇文章主要介紹了使用el-form之表單校驗(yàn)自動(dòng)定位到報(bào)錯(cuò)位置問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05vue?element-ui如何在el-tabs組件最右側(cè)添加按鈕
這篇文章主要介紹了vue?element-ui如何在el-tabs組件最右側(cè)添加按鈕問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07vue3.0父子傳參,子修改父數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了vue3.0父子傳參,子修改父數(shù)據(jù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04