另一个更新!!!
为了实现JS过渡可以看一下官方文档来学习。无论如何,我不会实现花哨的计时机制,但在最简单的形式中,React 中的 JS 折叠转换将如下所示,我将其包含在现有的代码框中:
<template>
...
<div>
<button @click="jsOpen = !jsOpen">Open JS list</button>
<transition
v-on:before-enter="beforeEnter"
v-on:enter="enter"
v-on:after-enter="afterEnter"
v-on:enter-cancelled="enterCancelled"
v-on:before-leave="beforeLeave"
v-on:leave="leave"
v-on:after-leave="afterLeave"
v-on:level-cancelled="leaveCancelled"
v-bind:css="false"
>
<ul v-if="jsOpen" class="mt-2 text-sm flex flex-col space-y-2">
<li v-for="i in 10" :key="i">item-{{ i }}</li>
</ul>
</transition>
</div>
</template>
<script>
export default {
...
methods: {
beforeEnter(el) {
el.style.height = 0;
el.style.overflow = "hidden";
},
enter(el, done) {
const increaseHeight = () => {
if (el.clientHeight < el.scrollHeight) {
const height = `${parseInt(el.style.height) + 5}px`;
el.style.height = height;
} else {
clearInterval(this.enterInterval);
done();
}
};
this.enterInterval = setInterval(increaseHeight, 10);
},
afterEnter(el) {},
enterCancelled(el) {
clearInterval(this.enterInterval);
},
beforeLeave(el) {},
leave(el, done) {
const decreaseHeight = () => {
if (el.clientHeight > 0) {
const height = `${parseInt(el.style.height) - 5}px`;
el.style.height = height;
} else {
clearInterval(this.leaveInterval);
done();
}
};
this.leaveInterval = setInterval(decreaseHeight, 10);
},
afterLeave(el) {},
leaveCancelled(el) {
clearInterval(this.leaveInterval);
},
},
};
</script>
更新:
在了解了 OP 的要求后,我试图玩不同的东西,我偶然发现了这篇关于 css-tricks的文章,其中详细解释了关于转换的更多细节,从那篇文章中提出的另一个非 js 解决方案如下所示,我将其包含在现有的stackblitz中:
.scale-enter-active,
.scale-leave-active {
transform-origin: top;
transition: transform 0.3s ease-in-out;
}
.scale-enter-to,
.scale-leave-from {
transform: scaleY(1);
}
.scale-enter-from,
.scale-leave-to {
transform: scaleY(0);
}
这里不是高度,而是针对 y 轴缩放。IT 还有一个副作用,它首先将容器扩展为完整大小,然后再更新(缩放)内容。
Vue v3.x
-leave
和V3-enter
中的类重命名为和。看看这个stackblitz。-leave-from
-enter-from
.slidedown-enter-active,
.slidedown-leave-active {
transition: max-height 0.5s ease-in-out;
}
.slidedown-enter-to,
.slidedown-leave-from {
overflow: hidden;
max-height: 1000px;
}
.slidedown-enter-from,
.slidedown-leave-to {
overflow: hidden;
max-height: 0;
}
Vue v2.x
问题是您指定max-height
为 100%,使用一些大于您预期的最大高度的值。将您的样式更新为:
.slide-down-enter-active,
.slide-down-leave-active {
transition: max-height 0.5s ease-in-out;
}
.slide-down-enter-to,
.slide-down-leave {
overflow: hidden;
max-height: 1000px;
}
.slide-down-enter,
.slide-down-leave-to {
overflow: hidden;
max-height: 0;
}
它应该可以工作。看看这个代码框演示。