d51*_*512 5 css transition vue.js
我有一个 Vue 应用程序,其中的元素需要通过单击按钮来展开和折叠。需要对效果进行动画处理以使其不那么刺耳。我为此使用了标签,但除非我在 css 中对<transition>元素的属性进行硬编码,否则它不起作用。height这是行不通的,因为该元素包含动态数据并且具有不同的高度。
这是我的代码:
<template>
<input type="button" value="Toggle" v-on:click="showIt = !showIt"/>
<transition name="test-tran">
<div class="test-block" v-show="showIt">
<div v-for="item in items">{{ item }}</div>
</div>
</transition>
</template>
<script>
export default {
data() {
return {
showIt: false
};
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
这是相应的CSS:
.test-block {
/*
adding a height makes the transition work but
an accurate height can't be known ahead of time
height: 100px;
*/
}
.test-tran-enter-active {
transition: all .5s ease;
}
.test-tran-leave-active {
transition: all .5s ease;
}
.test-tran-enter, .test-tran-leave-to
height: 0;
}
.test-tran-leave, .test-tran-enter-to
/*
adding a height makes the transition work but
an accurate height can't be known ahead of time
height: 100px;
*/
}
Run Code Online (Sandbox Code Playgroud)
如果没有该height属性,元素可以正确显示和隐藏,但没有动画。它只是立即出现并立即消失。
如何在不硬编码 CSS 高度的情况下使动画正常工作?