Vue2.0实现简单分页及跳转效果
(编辑:jimmy 日期: 2024/11/2 浏览:3 次 )
用Vue2.0实现一个数据的分页及页数的跳转,代码如下:
- 数据绑定:{{...}} <a v-on:click="btnClick(item)">{{item}}</a>
- 事件绑定:v-on <a v-on:click="cur--,pageClick()">上一页</a>
- 判断:v-if <li v-if="cur==1"><a class="banclick">上一页</a></li>
- 循环:v-for <li v-for="item in indexs" v-bind:class="{'active':cur==item}"></li>
- 修改样式:v-bind绑定class属性 <li v-for="item in indexs" v-bind:class="{'active':cur==item}"></li>
HTML代码如下:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>分页</title> <style> *{margin: 0;padding: 0;} #page-break{margin-top: 20px;margin-left: 20px;} #page-break li{list-style: none;} #page-break a{border: 1px solid #ddd; text-decoration: none;float: left;padding: 6px 12px;color: #337ab7;cursor: pointer} #page-break a:hover{background-color: #eee;} #page-break a .banclick{cursor: not-allowed;} #page-break .active a{color: #fff;cursor: default;background-color: #337ab7;border-color: #337ab7;} #page-break i{font-style: normal;color: #d44950;margin: 0px 4px;font-size: 12px;} #page-break .jumpbox .jumppage {border: 1px solid #ddd; margin-left: 40px; height: 33px; width: 40px; float: left;} #page-break .jumpbox .jumpbtn {cursor: pointer; margin-left: 10px;} #page-break .jumpbox .jumpbtn:active {color: #337ab7;} </style> <script src="/UploadFiles/2021-04-02/vue.js">js代码如下:
new Vue({ el: '#page-break', data: { cur: 1, all: 20 }, watch: { cur: function(newValue, oldValue){ console.log(arguments); } }, methods: { btnClick(num){ if(num!=this.cur){ this.cur=num; } }, pageClick(){ console.log('现在是'+this.cur+'页') }, pageSkip(){ var maxPage = this.all; var skipPage = Number(document.getElementsByClassName("jumppage")[0].value); console.log(typeof skipPage); if(!skipPage){ alert("请输入跳转页码"); return; }else if(skipPage<1 || skipPage>maxPage){ alert("您输入的页码超过页数范围了!"); return; }else{ //this.cur=skipPage; this.btnClick(skipPage); this.pageClick(); } } }, computed: { indexs(){ var left = 1; var right = this.all; var arr = []; if(this.all>=7){ if(this.cur>4 && this.cur<this.all-3){ left = this.cur-3; right = this.cur+3; }else if(this.cur<=4){ left=1; right=7; }else{ left=this.all-6; right=this.all; } } while(left<=right){ arr.push(left); left++; } return arr; } } })分页显示如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:JavaScript面向对象程序设计中对象的定义和继承详解