|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <meta http-equiv="X-UA-Compatible" content="ie=edge"> |
| 7 | + <title>vue-router</title> |
| 8 | + <!-- <link rel="stylesheet" href="css/amazeui.min.css"> --> |
| 9 | + <script src="./vue.js" ></script> |
| 10 | + <style> |
| 11 | + body{ |
| 12 | + font-family:"Segoe UI"; |
| 13 | + } |
| 14 | + li{ |
| 15 | + list-style:none; |
| 16 | + } |
| 17 | + a{ |
| 18 | + text-decoration:none; |
| 19 | + } |
| 20 | + .pagination { |
| 21 | + position: relative; |
| 22 | + |
| 23 | + } |
| 24 | + .pagination li{ |
| 25 | + display: inline-block; |
| 26 | + margin:0 5px; |
| 27 | + } |
| 28 | + .pagination li a{ |
| 29 | + padding:.5rem 1rem; |
| 30 | + display:inline-block; |
| 31 | + border:1px solid #ddd; |
| 32 | + background:#fff; |
| 33 | + |
| 34 | + color:#0E90D2; |
| 35 | + } |
| 36 | + .pagination li a:hover{ |
| 37 | + background:#eee; |
| 38 | + } |
| 39 | + .pagination li.active a{ |
| 40 | + background:#0E90D2; |
| 41 | + color:#fff; |
| 42 | + } |
| 43 | + </style> |
| 44 | +</head> |
| 45 | +<body> |
| 46 | + <script type="text/x-template" id="page"> |
| 47 | + <ul class="pagination" > |
| 48 | + <li v-show="current != 1" @click="current-- && goto(current)" ><a href="#">上一页</a></li> |
| 49 | + <li v-for="index in pages" @click="goto(index)" :class="{'active':current == index}" :key="index"> |
| 50 | + <a href="#" >{{index}}</a> |
| 51 | + </li> |
| 52 | + <li v-show="allpage != current && allpage != 0 " @click="current++ && goto(current++)"><a href="#" >下一页</a></li> |
| 53 | + </ul> |
| 54 | + </script> |
| 55 | + <div id="app"> |
| 56 | + <page></page> |
| 57 | + </div> |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +<script> |
| 64 | + |
| 65 | + Vue.component("page",{ |
| 66 | + template:"#page", |
| 67 | + data:function(){ |
| 68 | + return{ |
| 69 | + current:1, |
| 70 | + showItem:5, |
| 71 | + allpage:13 |
| 72 | + } |
| 73 | + }, |
| 74 | + computed:{ |
| 75 | + pages:function(){ |
| 76 | + var pag = []; |
| 77 | + if( this.current < this.showItem ){ //如果当前的激活的项 小于要显示的条数 |
| 78 | + //总页数和要显示的条数那个大就显示多少条 |
| 79 | + var i = Math.min(this.showItem,this.allpage); |
| 80 | + while(i){ |
| 81 | + pag.unshift(i--); |
| 82 | + } |
| 83 | + }else{ //当前页数大于显示页数了 |
| 84 | + var middle = this.current - Math.floor(this.showItem / 2 ),//从哪里开始 |
| 85 | + i = this.showItem; |
| 86 | + if( middle > (this.allpage - this.showItem) ){ |
| 87 | + middle = (this.allpage - this.showItem) + 1 |
| 88 | + } |
| 89 | + while(i--){ |
| 90 | + pag.push( middle++ ); |
| 91 | + } |
| 92 | + } |
| 93 | + return pag |
| 94 | + } |
| 95 | + }, |
| 96 | + methods:{ |
| 97 | + goto:function(index){ |
| 98 | + if(index == this.current) return; |
| 99 | + this.current = index; |
| 100 | + //这里可以发送ajax请求 |
| 101 | + } |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | +var vm = new Vue({ |
| 106 | + el:'#app', |
| 107 | + data:{ |
| 108 | + |
| 109 | + }, |
| 110 | + methods:{ |
| 111 | + |
| 112 | + add:function(index){ |
| 113 | + if(index >= this.allpage) return; |
| 114 | + this.current = ++index; |
| 115 | + }, |
| 116 | + reduce:function(index){ |
| 117 | + if(index <= 1) return; |
| 118 | + this.current = --index; |
| 119 | + }, |
| 120 | + getData:function(index){ |
| 121 | + console.log(index) |
| 122 | + } |
| 123 | + } |
| 124 | +}) |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +</script> |
| 129 | +</body> |
| 130 | +</html> |
0 commit comments