Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue.js Transitions & Animations

Vue.js Transitions & Animations

In this chapter we mainly discuss Vue.js transition effects and animation effects.


Transition

Vue provides multiple ways to apply transition effects when inserting, updating, or removing DOM elements.

Vue provides a built-in transition wrapper component used to wrap the component that needs the transition effect.

Syntax format



   

We can understand how Vue transitions are implemented through the following example:

Example

<div id="databinding">
<button v-on:click=">

show = !show

"

Click me

button

<

transition

name

=

"

fade

"

<

p

v-show

=

"

show

"

v-bind:style

=

"

styleobj

"

Animation example

p

transition

div

<script type="text/javascript">

var vm = new Vue({ el: '#databinding', data: { show:true, styleobj :{ fontSize:'30px', color:'red' } }, methods : { } });

script

Try it »

In the example, clicking the "Click Me" button changes the variable show from true to false. If true, the content of the child p tag is displayed.

The following code shows the transition tag wrapping a p tag:



    

动画实例

A transition is essentially a fade-in/fade-out effect. During the show/hide transition of an element, Vue provides 6 classes to switch:

For these class names switched during transitions, if you use an unnamed , then v- is the default prefix for these class names. If you use , then v-enter will be replaced by my-transition-enter.

v-enter-active and v-leave-active can control different easing curves for enter/leave transitions; an example is given in the following section.

CSS Transitions

Usually we use CSS transitions to achieve the effect.

The following example:

Example

<div id="databinding">
<button v-on:click=">

show = !show

"

Click me

button

<

transition

name

=

"

slide-fade

"

<

p

v-if

=

"

show

"

hello

p

transition

div

<

script

type

=

"

text/javascript

"

new Vue({ el: '#databinding', data: { show: true } })

script

Try it »

CSS Animations

CSS animations are used similarly to CSS transitions, but with animations the v-enter class is not removed immediately after the node is inserted into the DOM; instead it is removed when the animationend event fires.

Example

<div id="databinding">
<button v-on:click=">

show = !show

"

Click me

button

<

transition

name

=

"

bounce

"

<

p

v-if

=

"

show

"

Runoob Tutorial -- learn not only technology, but also dreams!!!

p

transition

div

<script type="text/javascript">

new Vue({ el: '#databinding', data: { show: true } })

script

Try it »

Custom transition class names

We can customize transition class names with the following attributes:

Custom transition class names take priority over the default ones, which makes it easy to combine with third-party animation libraries (such as animate.css).

Example

<div id="databinding">
<button v-on:click=">

show = !show

"

Click me

button

<

transition

name

=

"

custom-classes-transition

"

enter-active-class

=

"

animated tada

"

leave-active-class

=

"

animated bounceOutRight

"

<p v-if="show">

Runoob Tutorial -- learn not only technology, but also dreams!!!

p

transition

div

<script type="text/javascript">

new Vue({ el: '#databinding', data: { show: true } })

script

Try it »

Using transitions and animations together

To know when a transition has completed, Vue must set up the appropriate event listeners. It can be transitionend or animationend , depending on the CSS rules applied to the element. If you use either one, Vue can automatically detect the type and set up the listener.

However, in some scenarios you need to set two transition effects on the same element at the same time, for example animation is triggered and completes quickly, while transition the effect has not finished yet. In this case, you need to use the type attribute and set animation or transition to explicitly declare the type you want Vue to listen for.

Explicit transition duration

In many cases, Vue can automatically figure out when a transition finishes. By default, Vue waits for the first transitionend or animationend event on the root element of the transition. However, you can choose not to do so. For example, we can have a carefully orchestrated sequence of transitions where some nested inner elements have a delayed or longer transition than the root element.

In this case you can use the attribute on the component to duration customize an explicit transition duration (in milliseconds):

...

You can also customize the enter and leave durations:



...


JavaScript Hooks

JavaScript hooks can be declared in attributes:

HTML code:

<

transition

v-on:before-enter

=

"

beforeEnter

"

v-on:enter

=

"

enter

"

v-on:after-enter

=

"

afterEnter

"

v-on:enter-cancelled

=

"

enterCancelled

"

v-on:before-leave

=

"

beforeLeave

"

v-on:leave

=

"

leave

"

v-on:after-leave

=

"

afterLeave

"

v-on:leave-cancelled

=

"

leaveCancelled

"

transition

//

...

methods

:

{

//


//

Entering

//


beforeEnter

:

function

(

el

)

{

//

...

}

,

//

This callback function is optional

//

Used when combined with CSS

enter

:

function

(

el

,

done

)

{

//

...

done

(

)

}

,

afterEnter

:

function

(

el

)

{

//

...

}

,

enterCancelled

:

function

(

el

)

{

//

...

}

,

//


//

Leaving

//


beforeLeave

:

function

(

el

)

{

//

...

}

,

//

This callback function is optional

//

Used when combined with CSS

leave

:

function

(

el

,

done

)

{

//

...

done

(

)

}

,

afterLeave

:

function

(

el

)

{

//

...

}

,

//

leaveCancelled is only used with v-show

leaveCancelled

:

function

(

el

)

{

//

...

}

}

These hook functions can be used in combination with CSS transitions/animations, or on their own.

When using only JavaScript transitions,in enter and leave you must use done for the callback in. Otherwise, they will be called synchronously and the transition will complete immediately.

It is recommended to add the v-bind:css="false"attribute to elements that use only JavaScript transitions; Vue will skip CSS detection. This also avoids CSS interference during the transition.

A simple example using Velocity.js:

Example

<div id="databinding">
<button v-on:click=">

show = !show

"

Click me

button

<

transition

v-on:before-enter

=

"

beforeEnter

"

v-on:enter

=

"

enter

"

v-on:leave

=

"

leave

"

v-bind:css

=

"

false

"

<

p

v-if

=

"

show

"

Runoob Tutorial -- learn not only technology, but also dreams!!!

p

transition

div

<script type="text/javascript">

new Vue({ el: '#databinding', data: { show: false }, methods: { beforeEnter: function (el) { el.style.opacity = 0 el.style.transformOrigin = 'left' }, enter: function (el, done) { Velocity(el, { opacity: 1, fontSize: '1.4em' }, { duration: 300 }) Velocity(el, { fontSize: '1em' }, { complete: done }) }, leave: function (el, done) { Velocity(el, { translateX: '15px', rotateZ: '50deg' }, { duration: 600 }) Velocity(el, { rotateZ: '100deg' }, { loop: 2 }) Velocity(el, { rotateZ: '45deg', translateY: '30px', translateX: '30px', opacity: 0 }, { complete: done }) } } })

script

Try it »


Transitions on initial render

can be used via appear attribute to set a transition on the initial render of a node





  



Here it is the same as enter/leave transitions by default, and you can also customize the CSS class names.





  



Custom JavaScript hooks:





  



Transitions between multiple elements

We can set transitions for multiple elements, typically lists and descriptions:

Note that when switching between elements with the same tag name, you need to use the key attribute to set unique values to mark them so Vue can distinguish them; otherwise, for efficiency, Vue will only replace the content inside the same tag.



  

抱歉,没有找到您查找的内容。

The following example:



  

  



In some scenarios, you can also give the same element's key attribute different states instead of using v-if and v-else, and the example above can be rewritten as:



  



Using multiple v-if transitions of multiple elements can be rewritten as a single element transition with a dynamic attribute binding. For example:



  

  

  



can be rewritten as:



  





// ...

computed: {

  buttonMessage: function () {

    switch (this.docState) {

      case 'saved': return 'Edit'

      case 'edited': return 'Save'

      case 'editing': return 'Cancel'

    }

  }

}