如前所述 - maxheight 过渡通常是解决此问题的方法。
但在某些情况下,您可能无法使用 maxheight 过渡。
对于这些情况,您可以使用包装容器组件,该组件将在需要时进行转换。
<template>
<div
class="fluid-wrapper"
:class="{ 'in-transition': transitionState }"
:style="computedDimensions"
@transitionend="transitionState = 0"
>
<slot />
</div>
</template>
<script>
export default {
name: 'FluidContainer',
props: ['trigger'],
data() {
return {
oldRect: {
height: null,
width: null,
},
newRect: {
height: null,
width: null,
},
transitionState: 0,
// 0: no Dimensions, no transition
// 1: oldRect Dimensions, transition is on
// 2: newRect Dimensions, transition is on
};
},
computed: {
computedDimensions() {
if (!this.transitionState) {
return null;
}
return this.transitionState === 1 ? this.oldRect : this.newRect;
},
dimensionsHasChanged() {
return (
this.newRect.height !== this.oldRect.height
|| this.newRect.width !== this.oldRect.width
);
},
},
watch: {
trigger() {
const oldStyle = getComputedStyle(this.$el);
this.oldRect.height = oldStyle.height;
this.oldRect.width = oldStyle.width;
this.$nextTick(() => {
const newStyle = getComputedStyle(this.$el);
this.newRect.height = newStyle.height;
this.newRect.width = newStyle.width;
if (this.dimensionsHasChanged) {
this.transitionState = 1;
window.requestAnimationFrame(() => {
this.transitionState = 2;
});
} else {
this.transitionState = 0;
}
});
},
},
};
</script>
<style lang="scss" scoped>
.fluid-wrapper {
/* overflow: hidden; */
height: fit-content;
width: fit-content;
&.in-transition {
transition: all 0.3s;
}
}
</style>
用法:
<FluidContainer :trigger="some-variable">
<!-- Any Reactive Content -->
</FluidContainer>
'trigger' 道具 - 需要它才能工作。
它可以是使内部内容发生变化的任何状态。
包装器将监视触发器以检测何时发生尺寸变化并进行转换。