Vue.js 实现两个 Div 切换显示,并添加展开动画效果
可以使用 Vue 的 transition 组件来实现切换时的展开特效。
首先,定义两个 div,一个矮的,一个高的,并用 v-show 指令控制它们的显示和隐藏。
<template>
<div>
<div v-show='showLow' class='low'></div>
<div v-show='!showLow' class='high'></div>
</div>
</template>
然后,在 transition 组件中包裹这两个 div,并给它们添加一个类名,用于定义展开动画。
<template>
<div>
<transition name='expand'>
<div v-show='showLow' class='low'></div>
</transition>
<transition name='expand'>
<div v-show='!showLow' class='high'></div>
</transition>
</div>
</template>
最后,在样式中定义 .expand-enter 和 .expand-leave-to 类名来实现展开特效。
<style>
.expand-enter-active, .expand-leave-active {
transition: height 0.5s;
}
.expand-enter, .expand-leave-to {
height: 0;
opacity: 0;
}
.low {
height: 100px;
background-color: green;
}
.high {
height: 200px;
background-color: blue;
}
</style>
这样,当 showLow 变量改变时,Vue 会自动添加 expand-enter 和 expand-leave-to 类名来实现展开特效。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/ouqx 著作权归作者所有。请勿转载和采集!