基于vue.js的分頁(yè)插件詳解
Vue.js 的目標(biāo)是通過(guò)盡可能簡(jiǎn)單的 API 實(shí)現(xiàn)響應(yīng)的數(shù)據(jù)綁定和組合的視圖組件。想了解更多,請(qǐng)戳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>頁(yè)</a></li> </ul> </div>
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) } }
觀察了cur數(shù)據(jù)當(dāng)它改變的時(shí)候,可以獲取前后值。然后通知其他組件。
后期會(huì)在個(gè)人GitHub上提交完整版代碼
補(bǔ)充效果圖展示
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解項(xiàng)目升級(jí)到vue-cli3的正確姿勢(shì)
這篇文章主要介紹了詳解項(xiàng)目升級(jí)到vue-cli3的正確姿勢(shì),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Vue3中使用defineCustomElement 定義組件詳解
這篇文章主要為大家介紹了Vue3中使用defineCustomElement 定義組件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Vue監(jiān)聽(tīng)數(shù)據(jù)對(duì)象變化源碼
這篇文章主要為大家詳細(xì)介紹了Vue監(jiān)聽(tīng)數(shù)據(jù)對(duì)象變化的源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03vue導(dǎo)出excel文件流中文亂碼問(wèn)題及解決
這篇文章主要介紹了vue導(dǎo)出excel文件流中文亂碼問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06解決Vue 給mapState中定義的屬性賦值報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決Vue 給mapState中定義的屬性賦值報(bào)錯(cuò)的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Vue3組件不發(fā)生變化如何監(jiān)聽(tīng)pinia中數(shù)據(jù)變化
這篇文章主要給大家介紹了關(guān)于Vue3組件不發(fā)生變化如何監(jiān)聽(tīng)pinia中數(shù)據(jù)變化的相關(guān)資料,pinia是Vue的存儲(chǔ)庫(kù),它允許您跨組件/頁(yè)面共享狀態(tài),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11vue.js 左側(cè)二級(jí)菜單顯示與隱藏切換的實(shí)例代碼
這篇文章主要介紹了vue.js 左側(cè)二級(jí)菜單顯示與隱藏切換的實(shí)例代碼,需要的朋友可以參考下2017-05-05