這篇文章將為大家詳細(xì)講解有關(guān)vue.js中怎么實(shí)現(xiàn)一個(gè)分頁(yè)插件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
html代碼:
css部分,可根據(jù)自己的實(shí)際需要進(jìn)行調(diào)整:
.page-bar { margin-top: 21px; margin-left: 11%; } .page-bar ul, .page-bar li { margin: 0px; padding: 0px; } .page-bar ul li { list-style: none; border: 1px solid #ddd; text-decoration: none; position: relative; float: left; text-align: center; padding: 1px 0; margin-left: -1px; line-height: 1.42857143; color: #337ab7; cursor: pointer; width: 8%; } .page-bar li:first-child>a { margin-left: 0px } .page-bar .active a { color: #fff; cursor: default; background-color: #337ab7; border-color: #337ab7; } .page-bar i { font-style: normal; color: #d44950; margin: 0px 4px; font-size: 12px; }
js部分:
首先要?jiǎng)?chuàng)建一個(gè)基本組件
var vm = new Vue({ el: 'body', data: { list: null, all: 1, //總頁(yè)數(shù) cur: 1, //當(dāng)前頁(yè)碼 },
繼而要利用computed計(jì)算頁(yè)碼,
computed: { indexs: function(index) { var left = 1; var right = this.all; var ar = []; if (this.all >= 11) { if (this.cur > 5 && this.cur < this.all - 4) { left = this.cur - 5; right = this.cur + 4; } else { if (this.cur <= 5) { left = 1; right = 10; } else { right = this.all; left = this.all - 9; } } } while (left <= right) { ar.push(left); left++; } return ar; }, showLast: function() { if (this.cur == this.all) { return false } return true }, showFirst: function() { if (this.cur == 1) { return false } return true } }
要給 元素加v-on:click="cur++"事件,所以要在vue里加method方法:
methods: { btnClick: function(items) { //頁(yè)碼點(diǎn)擊事件 if (items != this.cur) { this.cur = items } } },
其實(shí)到這里基本上就差不多了,但是可以?xún)?yōu)化一下,當(dāng)用戶(hù)觸發(fā)點(diǎn)擊事件時(shí),頁(yè)面發(fā)生改變,這時(shí)要通知其他組件做出改變。
watch: { cur: function(oldValue, newValue) { console.log(arguments) } }
觀(guān)察了cur數(shù)據(jù)當(dāng)它改變的時(shí)候,可以獲取前后值。然后通知其他組件。
補(bǔ)充效果圖展示
關(guān)于vue.js中怎么實(shí)現(xiàn)一個(gè)分頁(yè)插件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。