当前位置:首页 > 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或主布局文件中引入并使用动画组件:

<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:

安装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>

带进度显示的启动动画

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

vue实现启动动画

<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按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue 实现滑动

vue 实现滑动

Vue 实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的<transition>组件结合CSS过渡或动画实现滑动效果…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…