vue实现slideup效果
实现 SlideUp 效果的 Vue 方法
使用 CSS Transition 和 Vue 指令
通过 Vue 的 v-show 或 v-if 结合 CSS Transition 实现滑动效果。定义一个 CSS 类控制元素高度和过渡效果。

<template>
<div>
<button @click="toggle">Toggle Slide</button>
<div class="slide-container" v-show="isVisible">
<div class="slide-content">
Content to slide up/down
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
isVisible: false
};
},
methods: {
toggle() {
this.isVisible = !this.isVisible;
}
}
};
</script>
<style>
.slide-container {
overflow: hidden;
transition: max-height 0.5s ease;
max-height: 0;
}
.slide-container.show {
max-height: 500px; /* 调整为实际内容高度 */
}
.slide-content {
padding: 10px;
background: #f0f0f0;
}
</style>
使用 Vue Transition 组件
Vue 内置的 <transition> 组件可以更灵活地控制动画效果。结合 CSS 定义滑动动画。

<template>
<div>
<button @click="toggle">Toggle Slide</button>
<transition name="slide">
<div v-show="isVisible" class="slide-content">
Content to slide up/down
</div>
</transition>
</div>
</template>
<script>
export default {
data() {
return {
isVisible: false
};
},
methods: {
toggle() {
this.isVisible = !this.isVisible;
}
}
};
</script>
<style>
.slide-enter-active, .slide-leave-active {
transition: all 0.5s ease;
max-height: 500px;
}
.slide-enter, .slide-leave-to {
max-height: 0;
opacity: 0;
overflow: hidden;
}
.slide-content {
padding: 10px;
background: #f0f0f0;
}
</style>
使用第三方库(如 Velocity.js)
对于更复杂的动画效果,可以集成 Velocity.js 或其他动画库。通过 Vue 的 transition 组件钩子实现。
<template>
<div>
<button @click="toggle">Toggle Slide</button>
<transition
@enter="enter"
@leave="leave"
:css="false"
>
<div v-show="isVisible" class="slide-content">
Content to slide up/down
</div>
</transition>
</div>
</template>
<script>
import Velocity from 'velocity-animate';
export default {
data() {
return {
isVisible: false
};
},
methods: {
toggle() {
this.isVisible = !this.isVisible;
},
enter(el, done) {
Velocity(el, {
maxHeight: '500px',
opacity: 1
}, {
duration: 500,
complete: done
});
},
leave(el, done) {
Velocity(el, {
maxHeight: '0',
opacity: 0
}, {
duration: 500,
complete: done
});
}
}
};
</script>
动态计算内容高度
如果需要根据内容动态调整高度,可以通过 JavaScript 计算内容高度并应用到动画中。
<template>
<div>
<button @click="toggle">Toggle Slide</button>
<div class="slide-container" :style="{ height: isVisible ? contentHeight + 'px' : '0' }">
<div ref="content" class="slide-content">
Content with dynamic height
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
isVisible: false,
contentHeight: 0
};
},
methods: {
toggle() {
this.isVisible = !this.isVisible;
if (this.isVisible) {
this.contentHeight = this.$refs.content.scrollHeight;
}
}
}
};
</script>
<style>
.slide-container {
overflow: hidden;
transition: height 0.5s ease;
}
.slide-content {
padding: 10px;
background: #f0f0f0;
}
</style>






