CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

31 December, 2020

Using Transitions and Animations Together

 Programing Coderfunda     December 31, 2020     Vue.js Tutorial     No comments   

Using Transitions and Animations Together

Vue.js requires attaching event listeners to make it known when a transition has ended. It may be transitionend or animationend, depending on the CSS you have applied. If you are using only first or only the second type, Vue.js can automatically detect the correct type. But, in the cases when you have to use both transitions and animations together, you may explicitly declare the type.

In some cases, you may want to have both on the same element, for example, having a CSS animation triggered by Vue.js, along with a CSS transition effect on hover. In these cases, you will have to explicitly declare the type you want Vue.js to care about in a type attribute, with a value of either animation or transition.

Explicit Transition Durations

This is a new feature introduced in the Vue.js version 2.2.0+. This is used to apply transition and animation both on the element using Vue.js. By default, Vue.js has to wait for the transionend and animationend event of detecting if the animation or transition is done. In such cases, when the transition causes delay, we can apply the duration explicitly as follows:

  1. <transition :duration = "1000"></transition>  

The duration property is used with the : symbol on the transition element, as you can see in the above code.

If you want to specify the duration separately for entering and leaving cases, you can specify it as follows:

  1. <transition :duration = "{ enter: 500, leave: 800 }">...</transition>   

JavaScript Hooks

The transition classes can be called as methods using JavaScript. You can define JavaScript hooks in attributes. Let's see an example to understand the concept well:

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Animation</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  6.         <script src = "https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script>  
  7.     </head>  
  8.     <body>  
  9.     <div id = "trans">  
  10.          <button @click = "show = !show">  
  11.             <span style = "font-size:25px;">Toggle</span>  
  12.          </button>  
  13.          <transition  v-on:before-enter = "beforeEnter"  
  14.             v-on:enter = "enter"  
  15.             v-on:leave = "leave"  
  16.             v-bind:css = "false">  
  17.             <p v-if = "show" style = "font-size:25px;">This is an animation Example with velocity</p>  
  18.          </transition>  
  19.       </div>  
  20.       </script>  
  21.       <script src="index.js"></script>  
  22.    </body>  
  23. </html>  

Index.js file:

  1. var vm = new Vue({  
  2.             el: '#trans',  
  3.             data: {  
  4.                show: false  
  5.             },  
  6.             methods: {  
  7.                beforeEnter: function (el) {  
  8.                   el.style.opacity = 0  
  9.                },  
  10.                enter: function (el, done) {  
  11.                   Velocity(el, { opacity: 1, fontSize: '25px' }, { duration: 1000 })  
  12.                   Velocity(el, { fontSize: '10px' }, { complete: done })  
  13.                },  
  14.                leave: function (el, done) {  
  15.                   Velocity(el, { translateX: '15px', rotateZ: '50deg' }, { duration: 1500 })  
  16.                   Velocity(el, { rotateZ: '100deg' }, { loop: 2 })  
  17.                   Velocity(el, {  
  18.                      rotateZ: '45deg',  
  19.                      translateY: '30px',  
  20.                      translateX: '30px',  
  21.                      opacity: 0  
  22.                   }, { complete: done })  
  23.                }  
  24.             }  
  25.          })  

Let's use a simple CSS file to make the output more attractive.

Index.css file:

  1. html, body {  
  2.     margin: 5px;  
  3.     padding: 0;  
  4. }  

After the execution of the program, you will see the following output:


Example Explanation

In the above example, an animation is performed using js methods on the transition element. The methods on transition are applied as follows:

  1. <transition  v-on:before-enter = "beforeEnter"  
  2.    v-on:enter = "enter"  
  3.    v-on:leave = "leave"  
  4.    v-bind:css = "false">  
  5.    <p v-if = "show" style = "font-size:25px;">Animation Example with velocity</p>  
  6. </transition>   

A prefix v-on is added before the name of the event. We have also added a property on transition v-bind:css = "false", so that Vue.js understands it as a JavaScript transition.

The methods are defined in the Vue.js instance as follows:

  1. methods: {  
  2.    beforeEnter: function (el) {  
  3.       el.style.opacity = 0  
  4.    },  
  5.    enter: function (el, done) {  
  6.       Velocity(el, { opacity: 1, fontSize: '25px' }, { duration: 1000 })  
  7.       Velocity(el, { fontSize: '10px' }, { complete: done })  
  8.    },  
  9.    leave: function (el, done) {  
  10.       Velocity(el, { translateX: '15px', rotateZ: '50deg' }, { duration: 1500 })  
  11.       Velocity(el, { rotateZ: '100deg' }, { loop: 2 })  
  12.       Velocity(el, {  
  13.          rotateZ: '45deg',  
  14.          translateY: '30px',  
  15.          translateX: '30px',  
  16.          opacity: 0  
  17.       }, { complete: done })  
  18.    }  
  19. }  

We have applied the required transition in each of the above methods. An opacity animation is applied on the click of the button and when the animation is done. A third party library is also used for animation.

Transition on Initial Render

If you want to add animation at the start, you have to add 'appear' property to the transition element. See the following example to understand it better:

Index.html file:

  1. <html>  
  2.    <head>  
  3.       <title>Vue.js Animation</title>  
  4.       <link rel="stylesheet" href="index.css">  
  5.       <link href = "https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel = "stylesheet" type = "text/css">  
  6.         <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  
  7.         <script src = "https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script>  
  8.     </head>  
  9.     <body>  
  10.         <div id = "trans_2" style = "text-align:center">  
  11.          <transition  
  12.             appear  
  13.             appear-class = "custom-appear-class"  
  14.             appear-active-class = "animated bounceIn">  
  15.             <h3>This is BounceIn Animation Example</h3>  
  16.          </transition>  
  17.          <transition  
  18.             appear  
  19.             appear-class = "custom-appear-class"  
  20.             appear-active-class = "animated swing">  
  21.             <h3>This is Swing Animation Example</h3>  
  22.          </transition>  
  23.          <transition  
  24.             appear  
  25.             appear-class = "custom-appear-class"  
  26.             appear-active-class = "animated rubberBand">  
  27.             <h3>This is RubberBand Animation Example</h3>  
  28.          </transition>  
  29.       </div>  
  30.       </script>  
  31.       <script src="index.js"></script>  
  32.    </body>  
  33. </html>  

Index.js file:

  1. var vm =  new Vue({  
  2.             el: '#trans_2',  
  3.             data: {  
  4.                show: true  
  5.             }  
  6.          })  

After the execution of the program, you will see the following output:


Sources by : coderfunda.tk

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Laravel Vue Flash Message From Scratch Today, I will tell you the best way to carry out streak message involving vue js in laravel 5 application. We will assembled vue streak message … Read More
  • How to get element by id in vue js? I will teach you how to get element by id in vuejs app. this example will help you to get element by id using refs in vue js app.If you are a us… Read More
  • Laravel Vue Router From Scratch Are you looking for create single page app using vue js in laraval then you know how to create router in vue. If you know about npm package then… Read More
  • Laravel Vue JS Document Transfer Utilizing vue-dropzone Model At the point when we expect to add simplified record transfer then we generally pick dropzone js for that since it is marvelous. So in this inst… Read More
  • Laravel Vue JS Picture Transfer Model with Demo Hello Folks,In this instructional exercise, I might want to impart to you how to picture transfer utilizing vue js laravel 5.6. We will picture … Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh
    I had follow these steps to install an configure firebase to my cordova project for cloud messaging. https://medium.com/@felipepucinelli/how...
  • Spring boot app (error: method getFirst()) failed to run at local machine, but can run on server
    The Spring boot app can run on the online server. Now, we want to replicate the same app at the local machine but the Spring boot jar file f...
  • Log activity in a Laravel app with Spatie/Laravel-Activitylog
      Requirements This package needs PHP 8.1+ and Laravel 9.0 or higher. The latest version of this package needs PHP 8.2+ and Laravel 8 or hig...
  • Step-by-step guide to linking gnuplot to Octave within Virtual Studio Code (VSC)
    I am aware of a number of previous questions (here, here and here for example) pointing out to the need to modify a file named .octaverc. ...
  • SQL Tutorial
    SQL Tutorial SQL HOME SQL Intro SQL Syntax SQL Select SQL Select Distinct SQL Where SQL And, Or, Not SQL Order By SQL Insert Into SQL Null V...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Track Metrics Effortlessly with Laravel's Context Increment and Decrement Methods - 5/4/2025
  • NativePHP Hit $100K — And We're Just Getting Started 🚀 - 5/8/2025
  • Name Queued Closures in Laravel 12.13 - 5/9/2025
  • Simplify HasManyThrough Relationships with Laravel's CanBeOneOfMany Support - 5/4/2025
  • Using Database Comments to Track Columns With Sensitive Data - 5/7/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda