[英]why the CSS transition doesn't work in the new dom?
當我使用CSS過渡時,如下所示。 https://jsfiddle.net/sunyuu/e58sfeva/17/
var Main = { data () { return { isActive: false, childStyle: {} }; }, methods: { showMask() { this.isActive = true this.$nextTick(() => { this.childStyle = { transform: 'scale(2, 2)' } }) }, hideMask() { this.childStyle = {} this.isActive = false } } } var Ctor = Vue.extend(Main) new Ctor().$mount('#app')
.parent { position: fixed; width: 100%; height: 100%; top: 0; background-color: rgba(0, 0, 0, 0.6); } .child { width: 100px; height: 150px; background-color: red; transition: transform .3s ease-in-out; }
<script src="//unpkg.com/vue/dist/vue.js"></script> <div id="app"> <template> <button @click="showMask">click here</button> <div class="parent" v-if="isActive" @click="hideMask"> <div v-bind:style="childStyle" class="child"></div> </div> </template> </div>
將元素添加到DOM時,過渡無效。 他們正在對CSS屬性的變化做出反應。
嘗試改用動畫。
@keyframes childScale { from {transform: scale(0, 0)} to {transform: scale(2, 2)} } .child { width: 100px; height: 150px; background-color: red; animation: childScale .3s ease-in-out; }
為了使CSS轉換有效,該dom必須可用,且不設置display:none
,並且應具有其轉換前的初始狀態值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.