Skip to content

xtf-collapse-animation

组件说明

xtf-collapse-animation 是折叠动画组件,用于内容展开/收起时的过渡动画。支持最大高度限制、透明度/缩放动画、懒渲染、销毁关闭、变更拦截和自动滚动,适用于手风琴面板、筛选面板和详情展开等场景。


基础用法

1. 最简示例

通过 v-modelshow 控制展开/收起:

vue
<template>
  <view>
    <xtf-button :label="expanded ? '收起' : '展开'" size="sm" @click="expanded = !expanded" />
    <xtf-collapse-animation v-model="expanded">
      <view style="padding: 24rpx; background: var(--xtf-color-surface)">
        <xtf-text level="body" text="这是可折叠的内容区域,展开时带有平滑的过渡动画。" />
      </view>
    </xtf-collapse-animation>
  </view>
</template>

<script>
export default {
  data() {
    return {
      expanded: false
    }
  }
}
</script>

2. 动画配置

通过 duration 设置动画时长,通过 easing 设置缓动函数,通过 animateOpacityanimateScale 控制附加动画:

vue
<template>
  <view>
    <xtf-button label="切换" size="sm" @click="show = !show" />
    <xtf-collapse-animation
      v-model="show"
      :duration="400"
      easing="ease-in-out"
      animate-opacity
      :animate-scale="true"
      :scale-from="0.96"
    >
      <view style="padding: 24rpx; background: var(--xtf-color-surface)">
        <xtf-text level="body" text="带透明度和缩放动画的折叠内容。" />
      </view>
    </xtf-collapse-animation>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: true
    }
  }
}
</script>

3. 懒渲染与销毁

通过 lazyRender 延迟渲染内容,通过 destroyOnClose 关闭时销毁内容:

vue
<template>
  <view>
    <xtf-button label="切换" size="sm" @click="visible = !visible" />
    <xtf-collapse-animation v-model="visible" lazy-render destroy-on-close>
      <view style="padding: 24rpx; background: var(--xtf-color-surface)">
        <xtf-text level="body" text="首次展开时才渲染,关闭后销毁 DOM。" />
      </view>
    </xtf-collapse-animation>
  </view>
</template>

<script>
export default {
  data() {
    return {
      visible: false
    }
  }
}
</script>

4. 变更拦截

通过 beforeChange 拦截展开/收起操作:

vue
<template>
  <xtf-collapse-animation v-model="open" :before-change="onBeforeChange">
    <view style="padding: 24rpx; background: var(--xtf-color-surface)">
      <xtf-text level="body" text="需要确认才能折叠的内容。" />
    </view>
  </xtf-collapse-animation>
</template>

<script>
export default {
  data() {
    return {
      open: true
    }
  },
  methods: {
    onBeforeChange({ nextExpanded }) {
      if (!nextExpanded) {
        uni.showModal({
          title: '提示',
          content: '确定要收起吗?',
          success: (res) => {
            if (res.confirm) {
              this.open = false
            }
          }
        })
        return false
      }
      return true
    }
  }
}
</script>

全部属性

属性类型默认值作用描述适用范围
showBooleantrue是否展开(Vue 2 v-model)控制展开
modelValueBooleanundefined是否展开(Vue 3 v-model)控制展开
durationNumber | String300动画时长(ms)动画速度
easingString'ease'缓动函数缓动效果
maxHeightNumber | String2400展开时最大高度(rpx)限制高度
collapsedHeightNumber | String0收起时高度(rpx)收起高度
autoScrollBooleanfalse展开时是否自动滚动到可视区自动滚动
scrollOffsetNumber | String0自动滚动偏移量(rpx)滚动偏移
scrollDurationNumber | String300滚动动画时长(ms)滚动速度
animateOpacityBooleantrue是否启用透明度动画透明度动画
animateScaleBooleanfalse是否启用缩放动画缩放动画
scaleFromNumber | String0.98缩放起始值缩放范围
disabledBooleanfalse是否禁用动画禁用动画
lazyRenderBooleanfalse是否懒渲染延迟渲染
destroyOnCloseBooleanfalse关闭时是否销毁内容销毁 DOM
beforeChangeFunctionnull变更拦截函数,返回 false 阻止变更拦截
customClassString''自定义类名样式覆盖
customStyleString | Object''自定义样式动态样式覆盖

事件

事件名称触发时机回调参数参数说明
open / close / opened / closed / change事件名已在 emits 声明-当前实现未调用 $emit,因此不会在运行时触发
inputVue 2 双向绑定更新(visible: Boolean)新的可见状态
update:showVue 3 双向绑定更新(visible: Boolean)新的可见状态
update:modelValueVue 3 双向绑定更新(visible: Boolean)新的可见状态

事件使用示例

vue
<template>
  <view>
    <xtf-button label="展开" @click="$refs.collapse.open()" />
    <xtf-button label="收起" @click="$refs.collapse.close()" />
    <xtf-collapse-animation
      ref="collapse"
      :show="expanded"
      @input="onVisibleChange"
      @update:show="onVisibleChange"
      @update:modelValue="onVisibleChange"
    >
      <view style="padding: 24rpx">可折叠内容</view>
    </xtf-collapse-animation>
  </view>
</template>

<script>
export default {
  data() {
    return { expanded: false }
  },
  methods: {
    onVisibleChange(value) {
      this.expanded = value
      console.log('展开状态:', value)
    }
  }
}
</script>

opencloseopenedclosedchange 虽已声明,但当前实现不会触发;请仅监听上表中的双向绑定更新事件。


方法

方法名参数返回值说明
open--展开内容
close--收起内容
toggleCollapse--切换展开/收起
refresh--刷新状态

主题说明

  • 折叠动画通过 max-height 过渡实现
  • 透明度动画通过 opacity 过渡实现
  • 缩放动画通过 transform: scale() 过渡实现
  • 动画时长和缓动函数可通过属性自定义

如需全局调整主题,请在 xtf-config-provider 或主题配置中覆盖相关 CSS 变量。

MIT Licensed