vuejs2.0实现一个简单的分页

前端开发 作者: 2024-08-20 20:05:01
用js实现的分页结果如图所示: css 模版 js 调用 new Vue({参数}) 就是创建了一个基本的组件,赋值给变量 pageBar.el就是element的缩写,定位模版的位置.data就是数
.page-bar{
    margin:40px;
}
ul,li{ 0px;
    padding: 0px;
}
li{
    list-style: none
}
.page-bar li:first-child>a {
   margin-left: 0px
}
.page-bar a{
    border: 1px solid #ddd;
    text-decoration: none;
    position: relative;
    float: left; 6px 12px;
    margin-left: -1px;
    line-height: 1.42857143;
    color: #337ab7;
    cursor: pointer
}
.page-bar a:hover{
    background-color: #eee;
}
.page-bar a.banclick{not-allowed;
}
.page-bar .active a{ #fff; default;
    border-color: #337ab7;
}
.page-bar i{
    font-style:normal; #d44950; 0px 4px;
    font-size: 12px;
}
<div class="page-bar">
    ul>
        li v-if="cur>1"><a v-on:click="cur--,pageClick()">上一页</a></li="cur==1"="banclick"v-for="index in indexs"  v-bind:class="{ 'active': cur == index}">
            ="btnClick(index)">{{ index }}="cur!=all"="cur++,1)">>下一页="cur == all">i>{{all}}>>
div>
var pageBar = new Vue({
    el: '.page-bar',data: {
        all: 20,//总页数
        cur: 1,1)">当前页码
});
computed: {
        indexs: function(){
          var left = 1;
          var right = this.all;
          var ar = [];
          if(this.all>= 5){
            this.cur > 3 && this.cur < this.all-2){
                    left = this.cur - 2
                    right = this.cur + 2
            }else{
                this.cur<=3){
                    left = 1
                    right = 5
                }{
                    right = .all
                    left = this.all -4
                }
            }
         }
        while (left <= right){
            ar.push(left)
            left ++
        }
        return ar
       }
         
    }
>
methods: {
        btnClick: function(data){页码点击事件
            if(data != .cur){
                this.cur = data 
            }
        },pageClick: (){
            console.log('现在在'+this.cur+'页');
        }
    },
watch: {
        cur: (oldValue,newValue){
            console.log(arguments);
        }
    }
<!DOCTYPE htmlhtmlheadmeta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
charset="utf-8"title="keywords"="" ="description"script type="text/javascript" src="js/vue.min.js"scriptstyle>
.page-bar{
    margin:40px;
} 0px;
    padding
li
    list-style none

.page-bar li:first-child>a 
   margin-left 0px

.page-bar a
    border 1px solid #ddd
    text-decoration none
    position relative
    float left 6px 12px
    margin-left -1px
    line-height 1.42857143
    color #337ab7
    cursor pointer

.page-bar a:hover
    background-color #eee
.page-bar a.banclicknot-allowed
.page-bar .active a #fff default
    border-color
.page-bar i
    font-stylenormal #d44950 0px 4px
    font-size 12px}
body>
 ="text/javascript">
var pageBar = new Vue({
    el: '.page-bar8//总页数
        cur: 1当前页码
    },watch: {
        cur: function(data){页码点击事件
            if(data != this.cur){
                .cur =(){
            console.log(现在在+.cur(){
           left ;
           right .all;
           ar  [];
          (.all>= 5){
            > 3 && < -2){
                    left - 
                    right + 
            }else{
                <=3
                }{
                    right .all
                    left .all 4
                }
            }
         }
        while (left  right){
            ar.push(left)
            left ++
        }
        return ar
       }
         
    }
})
>
View Code
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65696.html