当前位置:首页 > VUE

vue实现启动动画

2026-02-23 10:36:26VUE

实现启动动画的基本方法

在Vue中实现启动动画可以通过多种方式完成,常见的是利用Vue的过渡系统或结合CSS动画。以下是一种典型的实现方式:

创建动画组件 在components文件夹下新建LaunchAnimation.vue文件,内容如下:

<template>
  <transition name="launch" @after-enter="onAnimationComplete">
    <div v-if="show" class="launch-screen">
      <!-- 动画内容,可以是logo、加载条等 -->
      <div class="logo">Your Logo</div>
      <div class="loader"></div>
    </div>
  </transition>
</template>

<script>
export default {
  data() {
    return {
      show: true
    }
  },
  methods: {
    onAnimationComplete() {
      this.show = false
      this.$emit('animation-complete')
    }
  }
}
</script>

<style scoped>
.launch-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.logo {
  font-size: 2rem;
  margin-bottom: 2rem;
}

.loader {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.launch-enter-active, .launch-leave-active {
  transition: opacity 0.5s;
}
.launch-enter, .launch-leave-to {
  opacity: 0;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>

在应用中使用动画组件

在App.vue或主布局文件中引入并使用动画组件:

vue实现启动动画

<template>
  <div id="app">
    <LaunchAnimation @animation-complete="handleAnimationComplete" />
    <!-- 其他应用内容 -->
    <router-view />
  </div>
</template>

<script>
import LaunchAnimation from '@/components/LaunchAnimation.vue'

export default {
  components: {
    LaunchAnimation
  },
  methods: {
    handleAnimationComplete() {
      // 动画完成后的处理逻辑
    }
  }
}
</script>

基于路由的启动动画

如果需要仅在首次加载时显示启动动画,可以结合Vue Router实现:

<template>
  <div id="app">
    <LaunchAnimation 
      v-if="showLaunchAnimation" 
      @animation-complete="handleAnimationComplete" 
    />
    <router-view />
  </div>
</template>

<script>
import LaunchAnimation from '@/components/LaunchAnimation.vue'

export default {
  components: {
    LaunchAnimation
  },
  data() {
    return {
      showLaunchAnimation: true
    }
  },
  methods: {
    handleAnimationComplete() {
      this.showLaunchAnimation = false
    }
  },
  created() {
    // 检查是否首次访问
    if (sessionStorage.getItem('visited')) {
      this.showLaunchAnimation = false
    } else {
      sessionStorage.setItem('visited', 'true')
    }
  }
}
</script>

使用第三方动画库

对于更复杂的动画效果,可以结合第三方动画库如Animate.css:

vue实现启动动画

安装Animate.css:

npm install animate.css

修改动画组件:

<template>
  <transition
    enter-active-class="animate__animated animate__fadeIn"
    leave-active-class="animate__animated animate__fadeOut"
    @after-enter="onAnimationComplete"
  >
    <div v-if="show" class="launch-screen animate__animated animate__fadeIn">
      <!-- 动画内容 -->
    </div>
  </transition>
</template>

<script>
import 'animate.css'
export default {
  // 组件逻辑
}
</script>

带进度显示的启动动画

实现带加载进度的启动动画:

<template>
  <transition name="launch" @after-enter="onAnimationComplete">
    <div v-if="show" class="launch-screen">
      <div class="progress-bar">
        <div class="progress" :style="{ width: progress + '%' }"></div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  data() {
    return {
      show: true,
      progress: 0
    }
  },
  mounted() {
    const interval = setInterval(() => {
      this.progress += 10
      if (this.progress >= 100) {
        clearInterval(interval)
      }
    }, 300)
  },
  methods: {
    onAnimationComplete() {
      this.show = false
      this.$emit('animation-complete')
    }
  }
}
</script>

<style scoped>
.progress-bar {
  width: 80%;
  height: 20px;
  background: #f3f3f3;
  border-radius: 10px;
  overflow: hidden;
}

.progress {
  height: 100%;
  background: #42b983;
  transition: width 0.3s ease;
}
</style>

这些方法提供了不同复杂度的Vue启动动画实现方案,可以根据项目需求选择合适的实现方式。

标签: 动画vue
分享给朋友:

相关文章

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 html2canvas是一个流行的JavaScript库,可以将DOM元素转换为Canvas图像。在Vue项目中安装html2canvas: npm insta…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue实现选人

vue实现选人

实现选人功能的基本思路 在Vue中实现选人功能通常涉及以下核心环节:数据绑定、用户交互处理、状态管理以及界面渲染。以下是具体实现方法: 数据准备与组件结构 创建包含人员信息的数组,通常从API获取…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…