Home  >  Article  >  Web Front-end  >  How to use the transition-group component to implement list animation transition effects in Vue

How to use the transition-group component to implement list animation transition effects in Vue

WBOY
WBOYOriginal
2023-06-11 11:48:031992browse

Vue is a powerful front-end framework that provides many powerful components, including the transition-group component, which can help us achieve stunning list animation transition effects. In this article, we will introduce how to use Vue’s transition-group component to achieve these effects.

1. Introduction to transition-group component

Vue’s transition-group component is a component used to add animation effects when components change. It can contain multiple components or elements in a container and animate them. Through the transition-group component, we can achieve list animation transition, routing animation transition and other effects.

2. Implement the list animation transition effect

When implementing the list animation transition effect, we usually have the following steps:

  1. Create a list containing a list container and set a key for it;
  2. Use the v-for instruction to loop through each item in the rendering list;
  3. Set a key for each item;
  4. Use The transition-group component wraps this container;
  5. Set a name for this transition-group component;
  6. Define the entry and exit animation effects of list items in the style sheet.

Below we will introduce this process in detail through an example.

The sample code is as follows:

<template>
  <div>
    <button @click="addItem">添加</button>
    <button @click="removeItem">删除</button>
    <transition-group tag="ul" name="list">
      <li v-for="(item, index) in list" :key="item.id">{{ item.text }}</li>
    </transition-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { id: 1, text: "第一项" },
        { id: 2, text: "第二项" },
        { id: 3, text: "第三项" },
      ],
      nextId: 4,
    };
  },
  methods: {
    addItem() {
      this.list.push({ id: this.nextId++, text: `第${this.nextId}项` });
    },
    removeItem() {
      this.list.pop();
    },
  },
};
</script>

<style>
.list-enter-active,
.list-leave-active {
  transition: all 0.5s;
}
.list-enter,
.list-leave-to {
  opacity: 0;
}
</style>

First we define a list array in data, which contains three objects, each object has an id and text attributes. Then we use the v-for instruction in the template to loop through each item and set a key for each item. Note that here we use the transition-group component to wrap the list container and set a name attribute for it.

In the style sheet, we define the entry and exit animation effects for the list items. When entering, we set the opacity property to 0 to achieve the fade-in effect; when leaving, we also set the opacity property to 0 to achieve the fade-out effect.

If you run this sample code, you will see that each item in the list has a slow fade animation effect. And when you click the "Add" button, the newly added list items will also have the same animation effect. When you click the "Delete" button, the last item will also have the same animation effect.

3. Conclusion

In Vue, it is very simple to use the transition-group component to achieve the list animation transition effect. You only need to follow the above steps. With this example, we have a good grasp of this process. I hope readers can master this skill and use it in actual development.

The above is the detailed content of How to use the transition-group component to implement list animation transition effects in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn