javascript - VueJS - 是否可以禁用过渡组下特定元素的动画

标签 javascript vuejs2

我有一个表,它根据列表中的项目数量动态显示行。当将项目添加到列表中时,表行会动画化(具有绿色背景)以显示正在添加新行,当我从列表中删除项目时,表行会动画化(具有红色背景)以显示显示正在删除。我显示一行消息 There are no more items in the list当没有更多元素时。

我的问题是,当列表中没有更多项目时,列表为空时显示的表格行会使用我上面提到的绿色背景进行动画处理,当有项目时也是如此添加到列表中(从空),该行以红色背景动画

我的问题:是否可以忽略<transition-group>下的单个元素的动画?

HTML:

<table class="table table-striped" id="item-table">
    <thead>
        <tr>
            <th>Item</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody name="fade" is="transition-group">
        <tr v-for="(item, index) in items" :key="item.id">
            <td>
                {{ item.name }}
            </td>
            <td class="text-center">
                <button class="btn btn-primary" @click.prevent="removeItem(index, item.id)"><i class="fa fa-check"></i></button>
            </td>
        </tr>
        <tr v-if="items.length == 0" class="text-center" key="noItems">
            <td colspan="2">There are no more items in the list</td>
        </tr>
    </tbody>
</table>

CSS:

.fade-enter-active {
    transition: opacity 1.5s;
    background-color: #a1ec8e !important;
}

.fade-leave-active {
    transition: opacity 1.5s;
    background-color: tomato !important;
}

.fade-enter, .fade-leave-to {
    opacity: 0
}

JS:

const App = new Vue({
    el: "#app",

    data() {
        return {
            items: [],
        }
    },

    methods: {
        addItem(item) {
            this.items.push(item);
        },

        removeItem(index) {
            this.items.splice(index, 1);
        }
    }
});

最佳答案

从你的 CSS 中删除 !important。如果没有它,您可以使用如下内容覆盖行样式:

.fade-enter-active[key="noItems"] {
    background-color: yellow;
}

过渡组只是添加或删除子元素上的类,但动画样式仅由 css 控制。

关于javascript - VueJS - 是否可以禁用过渡组下特定元素的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430543/

相关文章:

javascript - 使用 getElementsByClassName 和 setAttribute 返回未定义的错误?

vuejs2 - 如何以编程方式确定 Buefy 输入字段是否有错误?

javascript - 从 axios 返回数据而不是 promise

javascript - Jquery PHP 图像 slider

javascript - 通过 Javascript 在 HTML 页面上使用 YouTube API

javascript - (覆盖 || 扩展)Javascript 方法

javascript - 在弹出窗口中显示 nvd3 饼图时出现问题?

vue.js 更改查询位置 NavigationDuplicated

javascript - 没有 Vue 加载器的 Vue 2 组件样式

javascript - 对对象属性的更改未反射(reflect)在 Vue 组件中