基于vue.js的分頁插件詳解
Vue.js 的目標是通過盡可能簡單的 API 實現(xiàn)響應(yīng)的數(shù)據(jù)綁定和組合的視圖組件。想了解更多,請戳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部分,可根據(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ù)當它改變的時候,可以獲取前后值。然后通知其他組件。
后期會在個人GitHub上提交完整版代碼
補充效果圖展示
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3中使用defineCustomElement 定義組件詳解
這篇文章主要為大家介紹了Vue3中使用defineCustomElement 定義組件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Vue3組件不發(fā)生變化如何監(jiān)聽pinia中數(shù)據(jù)變化
這篇文章主要給大家介紹了關(guān)于Vue3組件不發(fā)生變化如何監(jiān)聽pinia中數(shù)據(jù)變化的相關(guān)資料,pinia是Vue的存儲庫,它允許您跨組件/頁面共享狀態(tài),文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-11-11