uniapp/vue 实现循环列表内容的折叠展示简单效果 默认展示两个 超过两个隐藏

<template>
	<view class="content">
		<view class="list">
			<view class="list-item" v-for="(item,index) in list" :key="index">
				<view class="title">{{item.content}}</view>
				<!-- 展示隐藏内容的条件:  
					1、目标item的索引index等于点击项item的索引index  即:index==showIndex 
					2、flag的值为true-->
				<!-- 关闭隐藏内容的条件:
					1、flag的值为false
					ps:打开页面默认渲染class:item 所以不需要索引值index相等,
					同时我的需求是显示第一个item的隐藏内容的情况下,点击第二个item,
					第一个item的隐藏内容不消失,继续显示全部,直到我再次点击它关闭,-->
				<view :class="{active:index==showIndex && item.flag,item: !item.flag}">
					<view class="inner-item" v-for="(child,childIndex) in item.itemList" :key="childIndex">
						<view>{{child.info}}</view>
						<text class="iconfont iconxiangyou3 f24"></text>
					</view>
				</view>
				<!-- 子列表长度不超过2个的时候点击区域不显示  v-if="item.itemList.length>2"-->
				<view class="skip" @click="showAll(index)" v-if="item.itemList.length>2" >
					<text>{{item.flag?'收起':'展开全部'}}</text>
					<!--iconfont 引入的阿里巴巴矢量图标库  用自己引入的上下箭头的图标就行   -->
					<text class="iconfont" :class="item.flag?'iconshanghua':'iconxiahua'"></text>
				</view>
			</view>
		</view>

	</view>
</template>
<script>
	export default {
		data() {
			return {
				// 点击项的index索引
				showIndex: null,
				list: [{
						content: '主题内容1',
						//后端没有传回这个字段的话,在请求接口获得数据之后遍历数组给每一个item添加一个flag,默认值为false
						// this.list.forEach((item)=>{
						// 	item.flag = false
						// })
						flag: false,  
						itemList: [{
								info: '1'
							},
							{
								info: '2'
							},
							{
								info: '3'
							},
							{
								info: '4'
							}
						]
					},
					{
						content: '主题内容2',
						flag: false,
						itemList: [{
								info: 'a'
							},
							{
								info: 'b'
							},
							{
								info: 'c'
							},
							{
								info: 'd'
							}
						]
					},
					{
						content: '主题内容3',
						flag: false,
						itemList: [{
								info: 'I'
							},
							{
								info: 'II'
							}
						]
					}
				]

			}
		},
		onLoad() {},
		methods: {
			showAll(index) {
				this.showIndex = index
				this.list[index].flag = !this.list[index].flag
				console.log(this.showIndex, this.list[index].flag)
			}

		}
	}
</script>
<style lang="scss" scoped>
	page {
		background: #f1f1f1;
	}

	.list {
		margin: 20rpx;

		.list-item {
			padding: 20rpx;
			margin-bottom: 20rpx;
			background-color: #fff;
			border-radius: 10rpx;
			box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.04);

			.title {
				font-weight: 700;
				font-size: 32rpx;
				padding-bottom: 20rpx;
				margin-bottom: 20rpx;
				border-bottom: 1px solid #f1f1f1;
			}

			.item {
				height: 160rpx;
				overflow-y: hidden;

			}

			.active {
				height: 100%;
				transition: all 0.5s;
			}

			.inner-item {
				height: 80rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 20rpx;
				background-color: #f1f1f1;
				border-bottom: 1px dashed #fff;
			}

			.skip {
				margin-top: 20rpx;
				text-align: center;
				.iconfont{
					padding-left: 20rpx;
				}
			}
		}
	}
</style>

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一个简单折叠面板组件的代码示例,满足您的需求,以下是示例代码: ```html <template> <div class="collapse"> <div class="collapse-header" @click="toggleCollapse"> <slot name="title"></slot> </div> <transition name="collapse"> <div class="collapse-content" v-if="expanded" :style="{ height: contentHeight + 'px' }"> <slot name="content"></slot> </div> </transition> </div> </template> <script> export default { data() { return { expanded: false, contentHeight: 0 }; }, methods: { toggleCollapse() { this.expanded = !this.expanded; if (this.expanded) { this.contentHeight = this.$refs.content.clientHeight; } else { this.contentHeight = 0; } } } }; </script> <style scoped> .collapse { border: 1px solid #ddd; border-radius: 4px; overflow: hidden; } .collapse-header { padding: 10px; background-color: #f5f5f5; cursor: pointer; } .collapse-content { padding: 10px; overflow: hidden; transition: height 0.3s ease-out; } </style> ``` 使用时可以通过以下方式引入组件: ```html <template> <div> <collapse> <template #title> <h3>折叠面板标题1</h3> </template> <template #content> <p>折叠面板内容1</p> </template> </collapse> <collapse> <template #title> <h3>折叠面板标题2</h3> </template> <template #content> <p>折叠面板内容2</p> </template> </collapse> </div> </template> <script> import Collapse from '@/components/Collapse.vue'; export default { components: { Collapse } }; </script> ``` 请注意,示例代码中使用了 Vue 的过渡动画实现折叠展开时内容高度的动态变化。同时,为了避免组件内部样式影响到外部样式,使用了 scoped 属性对样式进行了作用域限制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值