In modern-day web applications, animations can add a lot of value to the user interface. Captivating animations can greatly increase the time a user spends on the website.
Vue.js provides built-in animation support to perform animations. In this shot, we will see how to animate lists using the animation features of Vue.js.
transition-group
componentVue.js provides a specific component, transition-group
, which is used to animate the insertion, deletion, shuffling, and so on of the elements rendered in a list.
This component contains an attribute name
that is used as the root component to define animations using CSS. It also contains an attribute tag
that specifies the type of HTML tag (element) to be rendered.
In this example, we'll do the following:
v-for
, and wrap it inside the transition-group
component.Now, let's look at the code:
div { text-align: center; } button { margin-right: 10px; border: none; border-radius: 5px; background-color: black; color: white; font-size: 16px; padding: 4px 8px; font-weight: 500; cursor: pointer; } .item { width: 150px; border: 1px solid gray; border-radius: 5px; box-shadow: 3px 5px 10px gray; padding: 5px 0; margin: 5px auto; list-style: none; } /* Adding animations for list items */ .list-enter-active { animation: fade-in 0.5s ease-in-out; } .list-leave-active { animation: fade-in 0.5s ease-in-out reverse; } @keyframes fade-in { 0% { opacity: 0; transform: translateX(30px); } 100% { transform: translateX(0px); } }
In the file Example.vue
:
pushItem()
function.popItem()
function.v-for
and wrap it inside the transition-group
component. We use list
and ul
as the name
and tag
attributes, respectively.Example.js
script.Example.css
stylesheet.In the file Example.js
:
data()
function.pushItem()
that pushes a new item into the list.popItem()
that pops the last item from the list.In the file Example.css
:
fade-in
using keyframes.Since we have used
name="list"
in thetransition-group
component, we have to uselist
as the root component in CSS, followed byenter-active
orleave-active
.
In the output, while pushing or popping a list item, we can see the fade-in animation.
RELATED TAGS
CONTRIBUTOR
View all Courses