Nuxt3 实战 (十一):添加路由 Transition 过渡效果和 Loading 动画

简介: 这篇文章介绍了Nuxt3框架中页面和布局的过渡效果设置方法,以及首屏加载动画的添加。通过配置nuxt.config.ts文件和添加CSS样式,可以实现页面过渡效果。同时,文章也提到了在页面中设置不同的过渡效果和为布局和页面同时设置过渡效果的方法。最后,文章以一个Github仓库链接和一个线上预览地址作为总结,表示遵循官方文档操作即可完成相关设置。

页面过渡效果

Nuxt3 利用 Vue组件 在页面和布局之间应用过渡效果。

  1. nuxt.config.ts 文件配置:
    export default defineNuxtConfig({
         
         
    app: {
         
         
     pageTransition: {
         
          name: 'page', mode: 'out-in' }
    },
    })
    
  2. 在页面之间添加过渡效果,在 app.vue 文件中添加以下 CSS

    <template>
    <NuxtPage />
    </template>
    
    <style>
    .page-enter-active,
    .page-leave-active {
          
          
    transition: all 0.4s;
    }
    .page-enter-from,
    .page-leave-to {
          
          
    opacity: 0;
    filter: blur(1rem);
    }
    </style>
    
  3. 要为页面设置不同的过渡效果,请在页面的 definePageMeta 中设置 pageTransition 键:

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

如果你同时更改布局和页面,这里设置的页面过渡效果将不会运行。相反,你应该设置布局过渡效果

布局过渡效果

  1. nuxt.config.ts 文件配置:
    export default defineNuxtConfig({
         
         
    app: {
         
         
     layoutTransition: {
         
          name: 'layout', mode: 'out-in' }
    },
    })
    
  2. app.vue 文件中添加代码:

    <template>
    <NuxtLayout>
    <NuxtPage />
    </NuxtLayout>
    </template>
    
    <style>
    .layout-enter-active,
    .layout-leave-active {
          
          
    transition: all 0.4s;
    }
    .layout-enter-from,
    .layout-leave-to {
          
          
    filter: grayscale(1);
    }
    </style>
    

首屏加载动画

Nuxt3 并没有直接提供 API,但我们利用 生命周期钩子 来完成我们期望的效果。

  1. 新建 components/FullLoading/index.vue 文件:

    <template>
    <div
      class="fixed flex w-screen h-screen justify-center items-center flex-col z-[99] overflow-hidden bg-white dark:bg-slate-900"
    >
      <div
        class="relative w-12 h-12 rotate-[165deg] before:content-[''] after:content-[''] before:absolute after:absolute before:top-2/4 after:top-2/4 before:left-2/4 after:left-2/4 before:block after:block before:w-[.5em] after:w-[.5em] before:h-[.5em] after:h-[.5em] before:rounded after:rounded before:-translate-x-1/2 after:-translate-x-1/2 before:-translate-y-2/4 after:-translate-y-2/4 before:animate-[loaderBefore_2s_infinite] after:animate-[loaderAfter_2s_infinite]"
      />
    </div>
    </template>
    <style>
    @keyframes loaderBefore {
          
          
    0% {
          
          
      width: 0.5em;
      box-shadow:
        1em -0.5em rgba(225, 20, 98, 0.75),
        -1em 0.5em rgba(111, 202, 220, 0.75);
    }
    
    35% {
          
          
      width: 2.5em;
      box-shadow:
        0 -0.5em rgba(225, 20, 98, 0.75),
        0 0.5em rgba(111, 202, 220, 0.75);
    }
    
    70% {
          
          
      width: 0.5em;
      box-shadow:
        -1em -0.5em rgba(225, 20, 98, 0.75),
        1em 0.5em rgba(111, 202, 220, 0.75);
    }
    
    100% {
          
          
      box-shadow:
        1em -0.5em rgba(225, 20, 98, 0.75),
        -1em 0.5em rgba(111, 202, 220, 0.75);
    }
    }
    
    @keyframes loaderAfter {
          
          
    0% {
          
          
      height: 0.5em;
      box-shadow:
        0.5em 1em rgba(61, 184, 143, 0.75),
        -0.5em -1em rgba(233, 169, 32, 0.75);
    }
    
    35% {
          
          
      height: 2.5em;
      box-shadow:
        0.5em 0 rgba(61, 184, 143, 0.75),
        -0.5em 0 rgba(233, 169, 32, 0.75);
    }
    
    70% {
          
          
      height: 0.5em;
      box-shadow:
        0.5em -1em rgba(61, 184, 143, 0.75),
        -0.5em 1em rgba(233, 169, 32, 0.75);
    }
    
    100% {
          
          
      box-shadow:
        0.5em 1em rgba(61, 184, 143, 0.75),
        -0.5em -1em rgba(233, 169, 32, 0.75);
    }
    }
    </style>
    
  2. app.vue 添加代码:

    <script setup lang="ts">
    const nuxtApp = useNuxtApp()
    
    // 是否首次加载
    const isFullLoading = ref(true)
    
    nuxtApp.hook('page:start', () => {
          
          
    isFullLoading.value = true
    })
    
    nuxtApp.hook('page:finish', () => {
          
          
    isFullLoading.value = false
    })
    </script>
    
    <template>
    <div>
      <!-- 首页加载全屏动画 -->
      <FullLoading v-if="isFullLoading" />
      <NuxtLayout>
        <NuxtPage />
      </NuxtLayout>
    </div>
    </template>
    

页面进度条

我们还可以添加一个页面进度条,Nuxt3 提供了 组件,我们直接在 app.vue 中添加:

<template>
  <NuxtLayout>
    <!-- 在页面导航之间显示一个进度条 -->
    <NuxtLoadingIndicator />
    <NuxtPage />
  </NuxtLayout>
</template>

效果预览

he1ge9mmznfop0ao19m887vhotzezmeq.gif

总结

通过本篇文章我们学习了如何在 Nuxt3 中添加路由切换过渡效果和首屏加载动画,没什么干货,按照官方文档操作就完事了。

Github 仓库dream-site

线上预览dream-site.cn

相关文章
|
1月前
|
JavaScript 前端开发 容器
< 每日小技巧: 基于Vue状态的过渡动画 - Transition 和 TransitionGroup>
Vue 的 `Transition` 和 `TransitionGroup` 是用于状态变化过渡和动画的组件。`Transition` 适用于单一元素或组件的进入和离开动画,而 `TransitionGroup` 用于 v-for 列表元素的增删改动画,支持 CSS 过渡和 JS 钩子。
< 每日小技巧: 基于Vue状态的过渡动画 - Transition 和 TransitionGroup>
|
1月前
|
前端开发 JavaScript
在Vue中,如何使用CSS过渡和动画来实现淡入淡出的效果?
在Vue中,如何使用CSS过渡和动画来实现淡入淡出的效果?
140 1
|
1月前
|
JavaScript
在Vue中,如何使用<transition>元素实现过渡效果?
在Vue中,如何使用<transition>元素实现过渡效果?
16 3
|
1月前
|
JavaScript 前端开发
vue中的css动画(过渡动画)使用步骤和原理
vue中的css动画(过渡动画)使用步骤和原理
|
8月前
|
前端开发 JavaScript
优美的Reactl列表动画:Styled-Components动画实践
优美的Reactl列表动画:Styled-Components动画实践
|
12月前
|
前端开发 JavaScript
【Vue3 第二十三章】Transition 过渡动画
【Vue3 第二十三章】Transition 过渡动画
211 1
|
10月前
|
JavaScript 前端开发
前端学习笔记202306学习笔记第五十二天-react.js & material-ui之构造数据,tabs和Grid部分的点击事件上之1
前端学习笔记202306学习笔记第五十二天-react.js & material-ui之构造数据,tabs和Grid部分的点击事件上之1
48 0
|
10月前
|
JavaScript 前端开发
前端学习笔记202306学习笔记第五十二天-react.js & material-ui之构造数据,tabs和Grid部分的点击事件上之2
前端学习笔记202306学习笔记第五十二天-react.js & material-ui之构造数据,tabs和Grid部分的点击事件上之2
38 0
|
10月前
|
JavaScript 前端开发
前端学习笔记202306学习笔记第五十二天-react.js & material-ui之构造数据,tabs和Grid部分的点击事件中之1
前端学习笔记202306学习笔记第五十二天-react.js & material-ui之构造数据,tabs和Grid部分的点击事件中之1
37 0
|
前端开发
Vue3——使用v-if或v-show来实现过渡的动画效果
使用v-if或v-show来实现过渡的动画效果
769 0