您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關vue.js中怎么實現(xiàn)一個分頁插件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
html代碼:
<div class="page-bar" v-else> <ul> <li v-if="showFirst"> <a v-on:click="cur--"> <<</a> </li> <li v-for="index in indexs" v-bind:class="{ 'active': cur == index}"> <a v-on:click="btnClick(index)">{{index}}</a> </li> <li v-if="showLast"><a v-on:click="cur++"> >></a></li> <li ><a>共<i>{{all}}</i>頁</a></li> </ul> </div>
css部分,可根據(jù)自己的實際需要進行調(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部分:
首先要創(chuàng)建一個基本組件
var vm = new Vue({ el: 'body', data: { list: null, all: 1, //總頁數(shù) cur: 1, //當前頁碼 },
繼而要利用computed計算頁碼,
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) { //頁碼點擊事件 if (items != this.cur) { this.cur = items } } },
其實到這里基本上就差不多了,但是可以優(yōu)化一下,當用戶觸發(fā)點擊事件時,頁面發(fā)生改變,這時要通知其他組件做出改變。
watch: { cur: function(oldValue, newValue) { console.log(arguments) } }
觀察了cur數(shù)據(jù)當它改變的時候,可以獲取前后值。然后通知其他組件。
補充效果圖展示
關于vue.js中怎么實現(xiàn)一個分頁插件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。