Vue分页简单功能的实现
这种方式适合数据量不是很大的页面分页
上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul{
margin: 0 auto;
list-style: none;
display: flex;
flex-direction: column;
align-items: center;
margin-top: 100px;
width: 380px;
height: 250px;
border: 1px solid rgb(109, 169, 184);
border-radius: 5px;
padding-bottom: 10px;
padding: 0px;
}
li{
width: 200px;
height: 35px;
line-height: 35px;
border-left: 3px solid rgb(230, 38, 25);
background-color: rgb(6, 111, 231);
margin-top: 10px;
color: white;
padding-left: 10px;
}
dl {
overflow: hidden;
display: flex;
justify-content: center;
}
dd {
width: 35px;
height: 35px;
line-height: 35px;
text-align: center;
background-color: rgb(151, 149, 149);
margin-left: 15px;
color: white;
font-size: 14px;
border-radius: 3px;
border: 2px soli #cccccc;
cursor: pointer;
}
dd:first-of-type,
dd:last-of-type{
width: 60px;
}
dd:hover {
background-color: rgba(37, 37, 37, 0.795);
color: white;
border: 2px soli #3f1717;
}
.active{
background-color: rgb(37, 37, 37);
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="item in show_page">{{item}}</li>
</ul>
<dl>
<dd @click="prev_page">上一页</dd>
<dd :class="{active:(index+1)==current_page}" @click="choice(index)" v-for="item,index in total_page">{{item}}</dd>
<dd @click="next_page">下一页</dd>
</dl>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el: "#app",
data: {
arr: [1,2,3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,21],
show_page:[],
page_num:5,
total_page:0,
current_page:1
},
methods: {
init(){
this.total_page=Math.ceil(this.arr.length/5);
this.show_page=this.arr.slice(0,this.page_num)
},
prev_page(){
if(this.current_page==1){
return
}
--this.current_page
this.show_page=this.arr.slice(this.page_num*(this.current_page-1),this.page_num*this.current_page)
},
next_page(){
if(this.current_page==this.total_page){
return
}
++this.current_page
this.show_page=this.arr.slice(this.page_num*(this.current_page-1),this.page_num*this.current_page)
},
choice(index){
this.current_page=index+1
this.show_page=this.arr.slice(this.page_num*(index),this.page_num*(index+1))
}
},
mounted:function(){
this.init()
}
});
</script>
</body>
</html>