Nuxt.js is a powerful framework for building Vue.js applications, and it comes with some fantastic features to enhance the user experience. One such feature is the ability to add transitions between pages and layouts, making your web application feel more dynamic and engaging. In this blog post, we’ll explore how to use page and layout transitions in Nuxt.js to create smooth and visually appealing transitions when navigating between pages or changing layouts.

What Are Page and Layout Transitions?

Page transitions in Nuxt.js are visual effects applied when navigating between different pages in your application. These transitions can include animations, fades, and other effects that make the page transition seamless.

Layout transitions, on the other hand, are applied when changing the layout of your application. Layout transitions can be used to smoothly transition between different layouts, such as switching from a default layout to a custom one.

Enabling Page Transitions

To enable page transitions for all your pages in a Nuxt.js application, you can configure them in your nuxt.config.ts file. Here’s an example of how to set up a page transition:

// nuxt.config.ts
export default defineNuxtConfig({
  app: {
    pageTransition: { name: 'page', mode: 'out-in' }
  },
})

In the above configuration, we’ve specified a page transition with the name “page” and the mode “out-in.” This means that when navigating between pages, the old page will transition out, and the new page will transition in.

Custom Page Transitions

You can also set custom page transitions for specific pages. For example, let’s say you want a 3D rotation effect for your “About” page. You can achieve this by using the definePageMeta function in your page’s script block:

// pages/about.vue
<script setup lang="ts">
definePageMeta({
  pageTransition: {
    name: 'rotate'
  }
})
</script>

With this configuration, navigating to the “About” page will apply the 3D rotation effect.

Enabling Layout Transitions

Layout transitions work similarly to page transitions but are applied when changing layouts in your application. To enable layout transitions globally, you can configure them in your nuxt.config.ts:

// nuxt.config.ts
export default defineNuxtConfig({
  app: {
    layoutTransition: { name: 'layout', mode: 'out-in' }
  },
})

This sets up a global layout transition with the name “layout” and the mode “out-in.”

Custom Layout Transitions

Just like with page transitions, you can set custom layout transitions for specific pages using definePageMeta:

// pages/about.vue
<script setup lang="ts">
definePageMeta({
  layout: 'orange',
  layoutTransition: {
    name: 'slide-in'
  }
})
</script>

In this example, navigating to the “About” page with the “orange” layout will apply the “slide-in” layout transition.

Global Settings and Disabling Transitions

You can customize default transition names globally for both page and layout transitions in your nuxt.config.ts file. Additionally, you can disable transitions for specific routes or globally if needed.

// nuxt.config.ts
export default defineNuxtConfig({
  app: {
    pageTransition: {
      name: 'fade',
      mode: 'out-in' // default
    },
    layoutTransition: {
      name: 'slide',
      mode: 'out-in' // default
    }
  }
})

To disable transitions for a specific route or globally, set pageTransition and layoutTransition to false.

// pages/some-page.vue
<script setup lang="ts">
definePageMeta({
  pageTransition: false,
  layoutTransition: false
})
</script>

Using JavaScript Hooks for Custom Transitions

For advanced use cases, you can use JavaScript hooks to create highly dynamic and custom transitions for your Nuxt pages. This allows you to define custom functions to control the transition behavior.

// pages/some-page.vue
<script setup lang="ts">
definePageMeta({
  pageTransition: {
    name: 'custom-flip',
    mode: 'out-in',
    onBeforeEnter: (el) => {
      console.log('Before enter...')
    },
    onEnter: (el, done) => {},
    onAfterEnter: (el) => {}
  }
})
</script>

These hooks enable you to have fine-grained control over the transition process.

Dynamic Transitions

You can apply dynamic transitions using conditional logic by leveraging inline middleware to assign different transition names based on conditions. This can create a dynamic and engaging user experience.

// pages/[id].vue
<script setup lang="ts">
definePageMeta({
  pageTransition: {
    name: 'slide-right',
    mode: 'out-in'
  },
  middleware (to, from) {
    to.meta.pageTransition.name = +to.params.id > +from.params.id ? 'slide-left' : 'slide-right'
  }
})
</script>

With this setup, the page applies different slide transitions depending on whether you’re navigating to a higher or lower ID.

Transition with NuxtPage

When using <NuxtPage /> in your app.vue, you can pass transition props directly to activate global transitions.

// app.vue
<template>
  <div>
    <NuxtLayout>
      <NuxtPage :transition="{
        name: 'bounce',
        mode: 'out-in'
      }" />
    </NuxtLayout>
  </div>
</template>

Remember that this page transition cannot be overridden with definePageMeta on individual pages.

View Transitions API (Experimental)

Nuxt.js offers an experimental implementation of the View Transitions API, which allows for native browser transitions between unrelated elements on different pages. You can enable this feature in your configuration file, but there are some considerations and known issues.

// nuxt.config.ts
export default defineNuxtConfig({
  experimental: {
    viewTransition: true
  }
})

Known Issues

View transitions may not work as expected with nested pages, layouts, or async components due to upstream Vue.js issues. If you encounter such issues, consider carefully whether to adopt this experimental feature.

If your pages involve data fetching within setup functions, you may need to reconsider using view transitions as they freeze DOM updates during transitions.


By implementing page and layout transitions in your Nuxt.js application, you can create a more engaging and visually appealing user experience. Whether you opt for global transitions or customize them for specific pages, Nuxt.js provides you with the tools to make your web application shine.

Write A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.