基于vue.js的分頁插件詳解
Vue.js 的目標(biāo)是通過盡可能簡(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>頁</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, //總頁數(shù)
cur: 1, //當(dāng)前頁碼
},
繼而要利用computed計(jì)算頁碼,
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) { //頁碼點(diǎn)擊事件
if (items != this.cur) {
this.cur = items
}
}
},
其實(shí)到這里基本上就差不多了,但是可以優(yōu)化一下,當(dāng)用戶觸發(fā)點(diǎn)擊事件時(shí),頁面發(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)文章
關(guān)于vue狀態(tài)過渡transition不起作用的原因解決
這篇文章主要介紹了關(guān)于vue狀態(tài)過渡transition不起作用的原因解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
vue中使用AJAX實(shí)現(xiàn)讀取來自XML文件的信息
這篇文章主要為大家詳細(xì)介紹了vue中如何使用AJAX實(shí)現(xiàn)讀取來自XML文件的信息,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考下2023-12-12
Elemenu中el-table中使用el-popover選中關(guān)閉無效解決辦法(最新推薦)
這篇文章主要介紹了Elemenu中el-table中使用el-popover選中關(guān)閉無效解決辦法(最新推薦),因?yàn)樵趀l-table-column里,因?yàn)槭嵌嘈?使用trigger="manual"?時(shí),用v-model="visible"來控制時(shí),控件找不到這個(gè)值,才換成trigger="click",需要的朋友可以參考下2024-03-03
Vue+Java 通過websocket實(shí)現(xiàn)服務(wù)器與客戶端雙向通信操作
這篇文章主要介紹了Vue+Java 通過websocket實(shí)現(xiàn)服務(wù)器與客戶端雙向通信操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
淺談vue使用axios的回調(diào)函數(shù)中this不指向vue實(shí)例,為undefined
這篇文章主要介紹了淺談vue使用axios的回調(diào)函數(shù)中this不指向vue實(shí)例,為undefined,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
antd form表單使用setFildesValue 賦值失效的解決
這篇文章主要介紹了antd form表單使用setFildesValue 賦值失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-04-04
vue使用elementUI分頁如何實(shí)現(xiàn)切換頁面時(shí)返回頁面頂部
這篇文章主要介紹了vue使用elementUI分頁如何實(shí)現(xiàn)切換頁面時(shí)返回頁面頂部,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue項(xiàng)目中實(shí)現(xiàn)的微信分享功能示例
這篇文章主要介紹了vue項(xiàng)目中實(shí)現(xiàn)的微信分享功能,結(jié)合實(shí)例形式分析了基于vue.js實(shí)現(xiàn)的微信分享功能具體定義與使用方法,需要的朋友可以參考下2019-01-01

