通過 Vue2.0 實現的分頁
可自由設置分頁顯示的多少、上一頁、下一頁、省略號等,也可直接輸入跳轉到的頁碼進行跳轉,分頁的樣式可自由調整
// 1、頁面的 head 部分,需要設計好頁面的樣式
1 .page { 2 font-weight: 900; 3 height: 40px; 4 text-align: center; 5 color: #888; 6 margin: 20px auto 0; 7 background: #f2f2f2; 8 } 9 10 .pagelist { 11 font-size: 0; 12 background: #fff; 13 height: 50px; 14 line-height: 50px; 15 } 16 17 .pagelist span { 18 font-size: 14px; 19 } 20 21 .pagelist .jump { 22 border: 1px solid #ccc; 23 padding: 5px 8px; 24 -webkit-border-radius: 4px; 25 -moz-border-radius: 4px; 26 border-radius: 4px; 27 cursor: pointer; 28 margin-left: 5px; 29 } 30 31 .pagelist .bgprimary { 32 cursor: default; 33 color: #fff; 34 background: #337ab7; 35 border-color: #337ab7; 36 } 37 38 .jumpinp input { 39 width: 55px; 40 height: 26px; 41 font-size: 13px; 42 border: 1px solid #ccc; 43 -webkit-border-radius: 4px; 44 -moz-border-radius: 4px; 45 border-radius: 4px; 46 text-align: center; 47 } 48 49 .ellipsis { 50 padding: 0px 8px; 51 } 52 53 .jumppoint { 54 margin-left: 30px; 55 } 56 57 .pagelist .gobtn {} 58 59 .bgprimary { 60 cursor: default; 61 color: #fff; 62 background: #337ab7; 63 border-color: #337ab7; 64 }
// 2、頁面的 body 部分,在多個 div 嵌套中放入多個 span 標簽來展示分頁
1 <div id="app"> 2 <div> 3 <div class="page" v-show="show"> 4 <div class="pagelist"> 5 <span class="jump"v-show="current_page>1" @click="{current_page--}">上一頁</span> 6 <span v-show="current_page>5" class="jump" @click="jumpPage(1)">1</span> 7 <span class="ellipsis" v-show="efont">...</span> 8 <span class="jump" v-for="num in indexs" :class="{bgprimary:current_page==num}" @click="jumpPage(num)">{{num}}</span> 9 <span class="ellipsis" v-show="efont&¤t_page<pages-4">...</span> 10 11 <span class="jump" @click="{current_page++}">下一頁</span> 12 <span v-show="current_page<pages-1" class="jump" class="jump" @click="jumpPage(pages)">{{pages}}</span> 13 14 <span class="jumppoint">跳轉到:</span> 15 <span class="jumpinp"><input type="text" v-model="changePage"></span> 16 <span class="jump gobtn" @click="jumpPage(changePage)">GO</span> 17 </div> 18 </div> 19 </div> 20 </div>
// 3、頁面底部,通過遠程調用 Vue 庫,或者引入下載好的 Vue.js,實例化分頁並且設置好參數
1 <script type="text/javascript" src="http://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> 2 <script> 3 var newlist = new Vue({ 4 el: '#app', 5 data: { 6 current_page: 1, //當前頁 7 pages: 50, //總頁數 8 changePage:'',//跳轉頁 9 nowIndex:0 10 }, 11 computed:{ 12 show:function(){ 13 return this.pages && this.pages !=1 14 }, 15 efont: function() { 16 if (this.pages <= 7) return false; 17 return this.current_page > 5 18 }, 19 indexs: function() { 20 21 var left = 1, 22 right = this.pages, 23 ar = []; 24 if (this.pages >= 7) { 25 if (this.current_page > 5 && this.current_page < this.pages - 4) { 26 left = Number(this.current_page) - 3; 27 right = Number(this.current_page) + 3; 28 } else { 29 if (this.current_page <= 5) { 30 left = 1; 31 right = 7; 32 } else { 33 right = this.pages; 34 35 left = this.pages - 6; 36 } 37 } 38 } 39 while (left <= right) { 40 ar.push(left); 41 left++; 42 } 43 return ar; 44 }, 45 }, 46 methods: { 47 jumpPage: function(id) { 48 this.current_page = id; 49 }, 50 }, 51 }) 52 </script>
展示效果: