Vue.js 的目標是通過盡可能簡單的 API 實現響應的數據綁定和組合的視圖組件。想了解更多,請戳http://cn.vuejs.org/
html代碼:
<div class="page-bar" v-else> <ul> <li style="width: 11%" 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 style="width: 11%" v-if="showLast"><a v-on:click="cur++"> >></a></li> <li style="width: 22%;margin-left: 7%"><a>共<i>{{all}}</i>頁</a></li> </ul> </div> css部分,可根據自己的實際需要進行調整:
.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部分:
首先要創建一個基本組件
var vm = new Vue({ el: 'body', data: { list: null, all: 1, //總頁數 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 } } }, 其實到這里基本上就差不多了,但是可以優化一下,當用戶觸發點擊事件時,頁面發生改變,這時要通知其他組件做出改變。
watch: { cur: function(oldValue, newValue) { console.log(arguments) } } 觀察了cur數據當它改變的時候,可以獲取前后值。然后通知其他組件。
后期會在個人GitHub上提交完整版代碼
補充效果圖展示

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答