Vue实现 展开 / 收起 功能

实现数据超过一定的量就显示 展开/ 收起 功能;如下图所示:

  

 参考代码:核心在于 计算属性 computed 中;

注意: 开发中key的值尽量不要使用下标 index;

// template:循环的时候直接⽤showHandleList进⾏操作,显⽰收起的事件直接⽤showAll = !showAll 进⾏控制,改变这个值的真假
<template>  
	<div class="hello">
	    <!-- 数据展示 -->
		<div v-for='(item, index) in showHandleList' :key="index">
			{{item}}
		</div>      
		<div
			class="show-more" 
			v-if="this.toLearnList.length > 3"
			@click="showAll = !showAll"
		>
			{{word}}
			<img
				class="score-img"
				:class="{ 'rotate-img': showAll }" 
				src="../static/img/ic_zhankai.png" 
				alt="logo"
			>
        </div>
	</div>
</template>  

<script>
export default {
	// ⾸先定义⼀下data⾥⾯的数据:
	data(){    
		return {      
			toLearnList:[ 'html','css','javascript','typescript','e-charts' ], //进⾏显⽰的数据
			howAll:false,   // 控制 展示 / 收起 的标志   
		}  
	},
	// 使⽤computed对data进⾏处理:
	computed:{    
		showHandleList(){      
			if(this.showAll == false){  //收起状态-显示“展示”        
				var showList = [];  //定义⼀个空数组        
				if(this.toLearnList.length > 3){  //控制显⽰前三个          
					for(var i = 0;i < 3; i++){            
						showList.push(this.toLearnList[i])  //将数组的前3条存放到showList数组中        
					}        
				}else{          
					showList = this.toLearnList;  //个数足够显示,不需要再截取        
				}        
				return showList;  //返回当前数组      
			}else{  // 展开状态-显示“收起”      
				return this.toLearnList;      
			}    
		},    
		word(){      
			if(this.showAll == false){  //对⽂字进⾏处理        
				return '展开'      
			}else{       
				return '收起'      
			}    
		}  
	},
}
</script>  

<style scoped>
.score-img {
  width: 16px;
  transition: all 0.3s;
}
.rotate-img {
  transform: rotate(180deg);
}
</style>

  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值