当前位置:首页 > VUE

vue实现加载动画

2026-02-17 23:20:30VUE

使用 CSS 动画实现加载动画

在 Vue 中可以通过 CSS 动画实现简单的加载效果。定义一个旋转的圆圈动画:

<template>
  <div class="loading-spinner"></div>
</template>

<style>
.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

使用第三方库实现复杂动画

Vue 可以集成第三方动画库如 animate.csslottie-web 实现更丰富的加载效果。以 animate.css 为例:

<template>
  <div class="animate__animated animate__bounce animate__infinite">
    Loading...
  </div>
</template>

<script>
import 'animate.css'
</script>

条件渲染加载动画

结合 Vue 的 v-ifv-show 指令,在数据加载时显示动画:

<template>
  <div>
    <div v-if="isLoading" class="loading-spinner"></div>
    <div v-else>
      <!-- 加载完成后的内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: true
    }
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false
    }, 2000)
  }
}
</script>

使用 Vue 过渡效果

Vue 的 <transition> 组件可以为加载动画添加过渡效果:

<template>
  <transition name="fade">
    <div v-if="isLoading" class="loading-spinner"></div>
  </transition>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

组件化加载动画

将加载动画封装为可复用组件:

<!-- LoadingSpinner.vue -->
<template>
  <div class="loading-spinner">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>
</template>

<style>
.loading-spinner {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.dot {
  width: 12px;
  height: 12px;
  background-color: #3498db;
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out;
}

.dot:nth-child(2) {
  animation-delay: 0.2s;
}

.dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}
</style>

骨架屏加载效果

实现内容加载前的骨架屏效果:

<template>
  <div class="skeleton-container">
    <div v-if="isLoading" class="skeleton">
      <div class="skeleton-header"></div>
      <div class="skeleton-line"></div>
      <div class="skeleton-line"></div>
    </div>
    <div v-else>
      <!-- 实际内容 -->
    </div>
  </div>
</template>

<style>
.skeleton {
  width: 100%;
  padding: 20px;
}

.skeleton-header {
  height: 40px;
  background-color: #e0e0e0;
  margin-bottom: 20px;
  border-radius: 4px;
}

.skeleton-line {
  height: 20px;
  background-color: #e0e0e0;
  margin-bottom: 10px;
  border-radius: 4px;
}
</style>

以上方法可以根据实际需求选择或组合使用,实现不同风格的加载动画效果。

vue实现加载动画

标签: 加载动画
分享给朋友:

相关文章

css3动画在线制作

css3动画在线制作

CSS3 动画在线制作工具 以下是一些常用的在线工具和平台,可以帮助快速生成 CSS3 动画代码,无需手动编写: Animista 网址:https://animista.net/ 特点:提供预设…

jquery加载

jquery加载

jQuery 加载方法 使用 CDN 加载 jQuery 推荐通过公共 CDN(如 Google、Microsoft 或 jQuery 官方)加载,速度快且可能已被浏览器缓存。 <scr…

jquery动画

jquery动画

jQuery动画基础 jQuery提供了多种动画方法,用于创建平滑的过渡效果。常用的动画方法包括animate()、fadeIn()、fadeOut()、slideUp()和slideDown()。…

uniapp 退场动画

uniapp 退场动画

实现退场动画的方法 在UniApp中实现退场动画可以通过CSS动画、Vue过渡效果或第三方动画库完成。以下是几种常见方法: 使用CSS动画 通过定义@keyframes和animation属性实…

vue实现路由动画

vue实现路由动画

路由动画的实现方法 在Vue中实现路由动画通常结合<transition>组件和Vue Router的导航守卫。以下是几种常见实现方式: 基础过渡动画 <template>…

js实现图片加载

js实现图片加载

使用Image对象加载图片 通过JavaScript的Image对象可以动态加载图片,适用于需要预加载或动态插入图片的场景。 const img = new Image(); img.src =…