[英]Why this vue transformation doesn't work as expected?
I'm trying to code simple slide panel, like here http://anton.shevchuk.name/wp-demo/jquery-tutorials/simple-slide-panel.html but on Vue.js. 我正在尝试对简单的幻灯片面板进行编码,例如http://anton.shevchuk.name/wp-demo/jquery-tutorials/simple-slide-panel.html,但在Vue.js上。 But panel doesn't slide it's wait 2 seconds then close without animation. 但是面板不会滑动,它会等待2秒钟,然后关闭而不显示动画。 https://codepen.io/TogusaRusso/pen/dqoMLr https://codepen.io/TogusaRusso/pen/dqoMLr
If I remove v-if, I can animate it, changing height of div. 如果删除v-if,则可以对其进行动画处理,从而更改div的高度。 https://codepen.io/TogusaRusso/pen/mGJEEJ https://codepen.io/TogusaRusso/pen/mGJEEJ
How can I animate removing of div? 如何为移除div设置动画?
<template>
<v-flex xs12 sm6 offset-sm3>
<transition name="slide" :duration="2000">
<div
class="slide-media"
:style="{height: '400px'}"
v-if="isOpen"
>Hello!</div>
</transition>
<v-btn
flat color="orange"
@click="isOpen=!isOpen"
>
{{isOpen?"Close":"Open"}}
</v-btn>
</v-flex>
</template>
<script>
export default {
name: 'SlidePanel',
data() {
return {
isOpen: false,
}
},
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.slide-media {
background-color: orangered;
overflow-y:hidden;
}
.slide-enter-active, .slide-leave-active {
transition: height 2s;
}
.slide-enter, .slide-leave-to {
height: 0px;
}
</style>
Check MDN: Css Specificity , 检查MDN:CSS特异性 ,
Inline styles added to an element (eg, style="font-weight:bold") always overwrite any styles in external stylesheets , and thus can be thought of as having the highest specificity. 添加到元素的内嵌样式 (例如,style =“ font-weight:bold”) 始终会覆盖 外部样式表中的任何样式 ,因此可以认为具有最高的特异性。
So removed the inline styles :style="{height: '400px'}"
, then add height:400px;
因此,删除了内联样式:style="{height: '400px'}"
,然后添加height:400px;
inside .slide-media
. 在.slide-media
内部。
Below is one demo: 以下是一个演示:
new Vue({ el: '#app', data(){ return { isOpen: false } }, methods: { togglePanel: function () { this.isOpen = !this.isOpen } } })
.slide-media { background-color: orangered; overflow-y:hidden; height:400px; } .slide-enter-active, .slide-leave-active { transition: height 2s; } .slide-enter, .slide-leave-to { height: 0px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script> <div id="app"> <button @click="togglePanel()">Toggle Panel</button> <transition name="slide" :duration="2000"> <div class="slide-media" v-if="isOpen" >Hello!</div> </transition> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.