uni-app 小程序 下拉框折叠 动画 简单实现

越风史
2023-12-01

通过动态设置展示区域的高度实现,初始定位0rpx;
在点击箭头的时候,动态赋值其真实高度,通过transition: all .38s 来实现

话不多说 直接上代码

<view :style="{ height: item.show ? `${(item.tableData.length * 84) - 2 }rpx` : '0' }" 
   class="content-region"
 >
  ****
</view>
 .content-region {
   height: 0rpx;
   overflow: hidden;
   transition: all .38s;
 }

没错,其实就这么简单就完事了,下面来个完整点的。

<template>
  <view class="reg-info">
    <view v-for="(item, index) in infoList" :key="index" class="item-class">
      <view class="title-region" @click="arrowClick(item)">
        <view class="title">{{ item.groupName }}</view>
        <view class="arrow-region">
          <view class="arrow" :style="{transform: item.show ? 'rotate(135deg)' : 'rotate(-45deg)'}"></view>
        </view>
      </view>
      <view :style="{ height: item.show ? `${(item.tableData.length * 84) - 2 }rpx` : '0' }" 
        class="content-region"
      >
        <view class="content-item" v-for="(it, ind) in item.tableData" :key="ind">
          <view class="item-title">{{it.title}}</view>
          <view class="item-value">{{it.value}}</view>
        </view>
      </view>
    </view>
  </view> 
</template>

<script>
export default {
  data() {
    return {
      infoList: [
        {
          groupName: '测试1',
          show: true,
          tableData: [
            {title: '123', value: '123'}
          ]
        },
                {
          groupName: '测试1', 
          show: true,
          tableData: [
            {title: '123', value: '123'}
          ]
        }
      ]
    }
  },
  methods: {
    arrowClick(item) {
      item.show = !item.show
    },
  }
}
</script>

<style lang="scss" scoped>
.reg-info {
  min-height: 100vh;
  background: #F6F7FA;
  padding: 40rpx 0 0 0;
  .item-class {
    &:nth-child(n+2) {
      margin: 40rpx 0 0 0;
    }
    .title-region {
      display: flex;
      justify-content: space-between;
      padding: 0 30rpx;
      position: relative;
      height: 45rpx;
      .title {
        font-size: 32rpx;
        font-weight: 600;
        color: #293F65;
        line-height: 45rpx;
      }
      .arrow-region {
        display: flex;
        align-items: center;
        .arrow {
          transition: all .3s ease-out;
          width: 15rpx;
          height: 15rpx;
          border-top: 4rpx solid #333;
          border-right: 4rpx solid #333;
        }
      }
    }
    .content-region {
      background: #ffffff;
      margin: 20rpx 0 0 0;
      height: 0rpx;
      overflow: hidden;
      transition: all .38s;
      .content-item {
        display: flex;
        align-items: center;
        height: 82rpx;
        padding: 0 30rpx;
        &:not(:last-child) {
          border-bottom: 2rpx solid #eeeeee;
        }
        .item-title {
          min-width: 128rpx;
          font-size: 30rpx;
          font-weight: 400;
          color: #293F65;
        }
        .item-value {
          margin: 0 0 0 30rpx;
          font-size: 30rpx;
          font-weight: 400;
          color: #8792A3;
        }
      }
    }
  }
}
</style>
 类似资料: