在类更改时Vue.js CSS过渡

Lea*_*eah 4 css-transitions vue.js

我正在尝试使用css transition属性#app通过添加类将背景从黑色逐渐淡化为白色.lightTheme.lightTheme类的添加按预期方式工作,但过渡效果不理想。

我是否必须添加Vue过渡元素?如果是这样怎么办?#app不会离开/进入dom-我只想为其属性设置动画(并且我不想在可能的情况下添加不必要的代码!

的HTML

<div id="app" v-bind:class="{ lightTheme: isActive }">
   <button v-on:click="toggleTheme">Change theme</button>
</div>
Run Code Online (Sandbox Code Playgroud)

JS

new Vue({
    el: '#app',

    data: {
        isActive: false
    },

    methods: {
      toggleTheme: function(){
          this.isActive = !this.isActive;
        // some code to filter users
      }
    }

});
Run Code Online (Sandbox Code Playgroud)

的CSS

#app {
    background: black;
    transition: 1s;
}

#app.lightTheme {
    background: white;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

weg*_*rer 8

要回答您的问题,如果您必须对此类情况使用过渡,答案为。您提出的解决方案应立即可用。

可能还有其他因素干扰您的代码,但是您发布的代码是正确的。

我附上了工作片段。

new Vue({
    el: '#app',
    data: {
        isActive: false
    },
    methods: {
      toggleTheme: function(){
          this.isActive = !this.isActive;
      }
    }
});
Run Code Online (Sandbox Code Playgroud)
#app {
    background: black;
    transition: 1s;
}

#app.lightTheme {
    background: white;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.min.js"></script>
<div id="app" :class="{ lightTheme: isActive }">
   <button @click="toggleTheme">Change theme</button>
</div>
Run Code Online (Sandbox Code Playgroud)