vue寫頁碼

用引入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ù)。

相關(guān)閱讀更多精彩內(nèi)容

  • (一) 晚風(fēng)習(xí)習(xí),清爽拂面。白潯坐在音樂噴泉旁的長椅上看著遠(yuǎn)處,那離她最近的那幢高層住宅樓,她曾經(jīng)住的那間房燈還未...
    楓幸閱讀 496評論 0 1
  • 春風(fēng)化雨,潤物無聲;愛生如子,甘為人梯一一一一聽上饒市優(yōu)秀教師先進(jìn)事跡演講報告會有感 10月16日,秋...
    五一雨昕兒閱讀 723評論 0 0
  • 辦公室里有位男同事,無意間聊起他很喜歡開車周游世界,我們被這充滿激情的夢想打動,為他喝彩時,他親口否定了自己的愛好...
    詠稻心閱讀 347評論 0 1

友情鏈接更多精彩內(nèi)容