Vue.js Animation
In Vue.js applications, we can apply animations in the same way as we applied transition in earlier examples. Animation also has classes that you have to declare to get the animation effect.
The only difference between Vue.js transition and Vue.js animation is that in Vue.js animation, the v-enter is not removed immediately after the element is inserted, but on an animation end event.
Let's take an example to understand the concept of animation and see how the animation works in an application.
Example
Index.html file:
- <html>
- <head>
- <title>Vue.js Animation</title>
- <link rel="stylesheet" href="index.css">
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
- </head>
- <body>
- <style>
- .shiftx-enter-active {
- animation: shift-in 2s;
- }
- .shiftx-leave-active {
- animation: shift-in 2s reverse;
- }
- @keyframes shift-in {
- 0% {transform:rotateX(0deg);}
- 25% {transform:rotateX(90deg);}
- 50% {transform:rotateX(120deg);}
- 75% {transform:rotateX(180deg);}
- 100% {transform:rotateX(360deg);}
- }
- </style>
- <div id = "databinding">
- <p> Click at the below button to see the animation effect.</p>
- <button v-on:click = "show = !show">Click Here</button>
- <transition name = "shiftx">
- <p v-show = "show">
- <img src = "https://www.flowerpower.com.au/media/catalog/product/image/287189f77f/love-you-rose.jpg" style = "width:100px;height:100px;" />
- </p>
- </transition>
- </div>
- </script>
- <script src="index.js"></script>
- </body>
- </html>
Index.js file:
- var vm = new Vue({
- el: '#databinding',
- data: {
- show:true
- },
- methods : {
- }
- })
Let's use a simple CSS file to make the output more attractive.
Index.css file:
- html, body {
- margin: 5px;
- padding: 0;
- }
After the execution of the program, you will see the following output
Example Explanation
In the above example, you can see that we have used classes same as transition effect. Here, we have enclosed an image in p tag as following:
- <transition name = "shiftx">
- <p v-show = "show">
- <img src = "https://www.flowerpower.com.au/media/catalog/product/image/287189f77f/love-you-rose.jpg" style = "width:100px;height:100px;" />
- </p>
- </transition>
Here, the name of the transition is shiftx and the class is applied as the following CSS code:
- <style>
- .shiftx-enter-active {
- animation: shift-in 2s;
- }
- .shiftx-leave-active {
- animation: shift-in 2s reverse;
- }
- @keyframes shift-in {
- 0% {transform:rotateX(0deg);}
- 25% {transform:rotateX(90deg);}
- 50% {transform:rotateX(120deg);}
- 75% {transform:rotateX(180deg);}
- 100% {transform:rotateX(360deg);}
- }
- </style>
In the above code, the class is defined within the transition name, i.e. shiftx-enter-active and .shiftx-leave-active.
The animation is defined with the keyframes from 0% to 100% where transform is defined at each of the keyframes in degree as following:
- @keyframes shift-in {
- 0% {transform:rotateX(0deg);}
- 25% {transform:rotateX(90deg);}
- 50% {transform:rotateX(120deg);}
- 75% {transform:rotateX(180deg);}
- 100% {transform:rotateX(360deg);}
- }
Custom Transition Classes
Vue.js facilitates you to specify your own custom transition classes by providing the following attributes. These attributes can be added to the transition element.
- enter-class
- enter-active-class
- enter-to-class (added in version 2.1.8+)
- leave-class
- leave-active-class
- leave-to-class (added in version 2.1.8+)
Custom classes are generally used when we want to use an external CSS library such as animate.css.
Let's take an example to understand the concept of custom transition classes and see how they work in an application.
Example
Index.html file:
- <html>
- <head>
- <title>Vue.js Animation</title>
- <link href = "https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel = "stylesheet" type = "text/css">
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
- <script type = "text/javascript" src = "js/vue.js"></script>
- </head>
- <body>
- <div id = "animation" style = "text-align:center">
- <button @click = "show = !show"><span style = "font-size:25px;">Click Here</span></button>
- <transition
- name = "custom-classes-transition"
- enter-active-class = "animated swing"
- leave-active-class = "animated bounceIn">
- <p v-if = "show"><span style = "font-size:25px;">See the animation effect</span></p>
- </transition>
- </div>
- </script>
- <script src="index.js"></script>
- </body>
- </html>
Index.js file:
- var vm = new Vue({
- el: '#animation',
- data: {
- show: true
- }
- })
After the execution of the program, you will see the following output:
When you click on the "Click Here" button, you can see two types of animations. The first animation is applied to the above example is:
- enter-active-class = "animated swing"
No comments:
Post a Comment
Thanks