vue.js中怎么實現(xiàn)一個分頁插件

這篇文章將為大家詳細講解有關(guān)vue.js中怎么實現(xiàn)一個分頁插件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

我們提供的服務(wù)有:網(wǎng)站設(shè)計、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、杭錦后ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的杭錦后網(wǎng)站制作公司

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, //當(dāng)前頁碼 
 },

繼而要利用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)化一下,當(dāng)用戶觸發(fā)點擊事件時,頁面發(fā)生改變,這時要通知其他組件做出改變。

 watch: { 
 cur: function(oldValue, newValue) { 
 console.log(arguments) 
 
 } 
 }

觀察了cur數(shù)據(jù)當(dāng)它改變的時候,可以獲取前后值。然后通知其他組件。

補充效果圖展示

vue.js中怎么實現(xiàn)一個分頁插件

關(guān)于vue.js中怎么實現(xiàn)一個分頁插件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享文章:vue.js中怎么實現(xiàn)一個分頁插件
分享網(wǎng)址:http://muchs.cn/article48/jpgehp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣網(wǎng)站內(nèi)鏈、網(wǎng)頁設(shè)計公司搜索引擎優(yōu)化、用戶體驗、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)