javascript - 为什么 CSS 过渡在新的 dom 中不起作用?

标签 javascript html css vue.js

当我像下面这样使用 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>
我希望红色立方体在 mask 显示时会缩放。 但是过渡不起作用,我无法弄清楚。 有人可以帮帮我吗?

最佳答案

当元素添加到 DOM 时,转换不起作用。他们对 CSS 属性的变化使用react。

尝试改用动画。

@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;
}

关于javascript - 为什么 CSS 过渡在新的 dom 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49047633/

相关文章:

html - 使用图标调整菜单大小而不会中断

javascript - 如何在 helper 中使用 unobustrive JS 重构 link_to_function ?

javascript - 通过中间件检查多权限

javascript - 如何仅处理子元素的委托(delegate)事件?

javascript - 如何防止两个 Canvas 对象出现在同一个位置

html - 响应式文本框布局

html - 如何在左侧和右侧放置具有相同边距的多个div元素?

javascript - ng-animate 不与 ng-show 一起使用

html - 如何在网站打开后显示弹出框

带有 CSS 翻译的 jQuery mousemove