Nuxt.js uses the transition component to let you create amazing transitions/animations between your routes.

To define a custom transition for a specific route add the transition key to the page component.

pages/index.vue

  1. export default {
  2. // Can be a String
  3. transition: ''
  4. // Or an Object
  5. transition: {}
  6. // or a Function
  7. transition (to, from) {}
  8. }

String

If the transition key is set as a string, it will be used as the transition.name.

pages/index.vue

  1. export default {
  2. transition: 'home'
  3. }

Nuxt.js will use these settings to set the component as follows:

pages/index.vue

  1. <transition name="home"></transition>

Transitions - 图1

This is automatically done for you and you do not need to add the <transition> component to your pages or layouts.

Now all you have to do is create the new class for your transitions.

pages/index.vue

  1. <style>
  2. .home-enter-active, .home-leave-active { transition: opacity .5s; }
  3. .home-enter, .home-leave-active { opacity: 0; }
  4. </style>

Object

If the transition key is set as an object:

pages/index.vue

  1. export default {
  2. transition: {
  3. name: 'home',
  4. mode: 'out-in'
  5. }
  6. }

Nuxt.js will use these settings to set the component as follows:

pages/index.vue

  1. <transition name="home" mode="out-in"></transition>

The transition object can have many properties such as name, mode, css, duration and many more. Please see the vue docs for more info.

You can also define methods in the page transition property, for more information on the JavaScript hooks see the vue docs.

  1. export default {
  2. transition: {
  3. afterLeave(el) {
  4. console.log('afterLeave', el)
  5. }
  6. }
  7. }

Transition Mode

Transitions - 图2

The default transition mode for pages differs from the default mode in Vue.js. The transition mode is by default set to out-in. If you want to run leaving and entering transitions simultaneously, you have to set the mode to the empty string mode: ''.

pages/index.vue

  1. export default {
  2. transition: {
  3. name: 'home',
  4. mode: ''
  5. }
  6. }

Function

If the transition key is set as a function:

pages/index.vue

  1. export default {
  2. transition(to, from) {
  3. if (!from) {
  4. return 'slide-left'
  5. }
  6. return +to.query.page < +from.query.page ? 'slide-right' : 'slide-left'
  7. }
  8. }

Transitions applied on navigation:

/ to /posts => slide-left,/posts to /posts?page=3 => slide-left,/posts?page=3 to /posts?page=2 => slide-right.

Global Settings

The Nuxt.js default transition name is "page". To add a fade transition to every page of your application, all you need is a CSS file that is shared across all your routes.

Our global css in assets/main.css:

assets/main.css

  1. .page-enter-active,
  2. .page-leave-active {
  3. transition: opacity 0.5s;
  4. }
  5. .page-enter,
  6. .page-leave-to {
  7. opacity: 0;
  8. }

Then we add its path to the css array in our nuxt.config.js file:

nuxt.config.js

  1. export default {
  2. css: ['~/assets/main.css']
  3. }

Configuration Settings

The layoutTransition Property

The layout transition is used to set the default properties of the layout transitions.

The default settings for layout transitions are:

  1. {
  2. name: 'layout',
  3. mode: 'out-in'
  4. }

assets/main.css

  1. .layout-enter-active,
  2. .layout-leave-active {
  3. transition: opacity 0.5s;
  4. }
  5. .layout-enter,
  6. .layout-leave-active {
  7. opacity: 0;
  8. }

If you want to change the default settings for your layout transitions you can do so in the nuxt.config.js file.

nuxt.config.js

  1. export default {
  2. layoutTransition: 'my-layouts'
  3. // or
  4. layoutTransition: {
  5. name: 'my-layouts',
  6. mode: 'out-in'
  7. }
  8. }

assets/main.css

  1. .my-layouts-enter-active,
  2. .my-layouts-leave-active {
  3. transition: opacity 0.5s;
  4. }
  5. .my-layouts-enter,
  6. .my-layouts-leave-active {
  7. opacity: 0;
  8. }

The pageTransition Property

The default settings for page transitions are:

  1. {
  2. name: 'page',
  3. mode: 'out-in'
  4. }

Should you wish to modify the default settings you can do so in the nuxt.config.js

nuxt.config.js

  1. export default {
  2. pageTransition: 'my-page'
  3. // or
  4. pageTransition: {
  5. name: 'my-page',
  6. mode: 'out-in',
  7. beforeEnter (el) {
  8. console.log('Before enter...');
  9. }
  10. }
  11. }

If you do modify the page Transition name you will also have to rename the css class.

assets/main.css

  1. .my-page-enter-active,
  2. .my-page-leave-active {
  3. transition: opacity 0.5s;
  4. }
  5. .my-page-enter,
  6. .my-page-leave-to {
  7. opacity: 0;
  8. }