vue实现的分页组件(进行了小修改)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		
	</head>
	<style>
			li{
				list-style: none;
				display: inline-block;
			}
			span{
				display: inline-block;
				width: 30px;
				height: 30px;
				border: 1px solid indianred;
				line-height: 30px;
				text-align: center;
			}
			span:hover{
				background-color: #DDDDDD;
				cursor: pointer;
			}
			[v-cloak] {
			  	display: none;
			}
			.active{
				background-color: deepskyblue;
				color: #FFFFFF;
			}
		</style>
	
	<body>
		
		<div id="page">
			<ul >
				<li v-show="current!=1" @click="current--"><span><<</span></li>
				<li  v-for="index in pages" @click="current=index" :class="{'active':current==index}"><span v-cloak>{{index}}</span></li>
				<li v-show="current!=total && total!=0" @click="current++"><span>>></span></li>
			</ul>
		</div>
	
	</body>
	
	<script src="https://cdn.jsdelivr.net/npm/vue"></script>
	<script>
	new Vue({
		el:'#page',
		data:{
			current:1, //当前页
			total:12,   //总页数
			showItem:5  //最多展示的页码数
		},
		computed:{
			pages(){
			    var page=[];//显示页码
			    //思想:用当前激活页面驱动页面显示的分别
			    if(this.current<this.showItem){//当前页小于最大页码数(showItem),区分总页数是否达到最大页码数
			    	//获取总页数和最大页码数较小的值
			    	var i=Math.min(this.total,this.showItem);
			    	while(i){
			    		//通过page的数组值显示页码
			    		page.unshift(i--);
			    	}
			    }else{//当前页面大于最大页码数(showItem)时,区分显示的页码规则
			    	var pagestart=this.current-Math.floor(this.showItem/2);//获取显示的页码第一位页码(默认当前页居中)
			    	var i=this.showItem;//用来显示多少(i)个页码
			    	if(pagestart>(this.total-this.showItem)){//第一个页码如果大于总页数减去展示的页码数,则当前页不能居中
			    		pagestart=(this.total-this.showItem)+1;//应该显示的第一个页码数
			    	}
			    	while(i--){
			    		//通过page的数组值显示页码
			    		page.push(pagestart++);
			    	}
			    }
			    return page;
			}
		}
	})
</script>
</html>

 

效果图如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值