用引入vue.js的方法做頁碼
<script type="text/x-template" id="page">
<ul class="pagination" >
<li @click="goto(1)" ><a href="#">首頁</a></li>
<li v-show="current != 1" @click="current-- && goto(current)" ><a href="#">上一頁</a></li>
<li v-for="index in pages" @click="goto(index)" :class="{'active':current == index}" :key="index">
<a href="#" >{{index}}</a>
</li>
<li v-show="allpage != current && allpage != 0 " @click="current++ && goto(current++)"><a href="#" >下一頁</a></li>
<li v-show="allpage > showItem" @click="goto(allpage)" ><a href="#">尾頁</a></li>
</ul>
</script>
<div id="app">
<page></page>
</div>
<script>
Vue.component("page",{
template:"#page",
data:function(){
return{
current:1,
showItem:5,
allpage:13
}
},
computed:{
pages:function(){
var pag = [];
if( this.current < this.showItem ){ //如果當(dāng)前的激活的項(xiàng) 小于要顯示的條數(shù)
//總頁數(shù)和要顯示的條數(shù)那個大就顯示多少條
var i = Math.min(this.showItem,this.allpage);
while(i){
pag.unshift(i--);
}
}else{ //當(dāng)前頁數(shù)大于顯示頁數(shù)了
var middle = this.current - Math.floor(this.showItem / 2 ),//從哪里開始
i = this.showItem;
if( middle > (this.allpage - this.showItem) ){
middle = (this.allpage - this.showItem) + 1
}
while(i--){
pag.push( middle++ );
}
}
return pag
}
},
methods:{
goto:function(index){
if(index == this.current) return;
this.current = index;
//這里可以發(fā)送ajax請求
}
}
})
var vm = new Vue({
el:'#app',
})
</script>
實(shí)現(xiàn)效果請見:demo
點(diǎn)擊 下載源碼
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。