前端开发小技巧 - 【Vue】+【CSS】 - 加载动画

前言

  • 本文是基于 【Vue3 + Vite】写的;
    • Vue2要使用的话,只需对一些基本语法进行更改即可使用;
  • 我是将这个加载动画封装成了一个组件,父组件通过传递一些属性进行样式定义;
    • 我会在下文中对每个属性进行标注,方便大家参考;
  • 效果展示:
  • 本文的代码是跟着抖音上一位大佬敲的😂😂(我忘记那个Up主的昵称是啥了😂😂);

一、将加载动画封装成一个组件

html 复制代码
<script setup>
defineProps({
  // 控制加载动画盒子的大小、背景色
  loadingStyle: {
    type: Object,
    default: () => ({
      width: "500px",
      height: "200px",
      backgroundColor: "#252839"
    })
  },
  // 控制动画字体的大小和颜色
  textStyle: {
    type: Object,
    default: () => ({
      color: "#252839",
      fontSize: "100px"
    })
  },
  // 动画时长
  animationTime: {
    type: String,
    default: "3s"
  }
});
</script>

<template>
  <div class="loading" :style="loadingStyle">
    <!-- 如果要对此处的加载动画的文本进行修改,也需要对下面伪元素中的 content 属性值进行修改,保持两者一致 -->
    <h2 class="loading-text" :style="textStyle">Loading...</h2>
  </div>
</template>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.loading {
  display: flex;
  justify-content: center;
  align-items: center;
}

.loading-text {
  position: relative;
  -webkit-text-stroke: 0.2vw #b6b6b6; /*文字描边效果*/
}

.loading-text::before {
  position: absolute;
  top: 0;
  left: 0;
  content: "Loading...";
  width: 100%;
  height: 100%;
  border-right: 2px solid #01fe87;
  color: #01fe87;
  -webkit-text-stroke: 0vw #383d25;
}

.loading-text::before {
  width: 0;
  overflow: hidden;
  animation: animate v-bind(animationTime) linear infinite alternate;
}

@keyframes animate {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}
</style>

二、使用组件

html 复制代码
<script setup>
import Loading from "@/views/Loading.vue";
</script>

<template>
  <Loading :loadingStyle="{ width: '600px', height: '300px', backgroundColor: '#252839' }" />
</template>

<style scoped></style>
相关推荐
wordbaby3 分钟前
为什么 Apifox 能调通,浏览器却报跨域?一次“CORS Error + 403”联调排坑实录
前端
2603_965896629 分钟前
JavaScript 原型与原型链
开发语言·前端·javascript
Amos_Web10 分钟前
通过 Rspack 源码学 Rust(四):用 `Option<T>` 表达“查询可能没有结果”
前端
朋友圈自动点赞工具16 分钟前
Ruby方法查找:对象不存方法
开发语言·前端·ruby·科技资讯
晓得迷路了20 分钟前
栗子前端技术周刊第 152 期 - Rspack 2.3、TypeScript 7.1 Beta、Bun 1.4.4...
前端·javascript·css
普通人631 分钟前
CSS-box-sizing-使用指南
开发语言·前端·css·1024程序员节
风骏时光牛马36 分钟前
AI开源工具一站式尝鲜体验
前端
IT_陈寒44 分钟前
Python项目实战经验分享:为什么你的多进程日志会丢失?
前端·人工智能·后端
柳杉1 小时前
让 AI 学会审美、建模和剪片:我收藏夹里的 14 个仓库
前端·ai编程·three.js
前端架构师-老李1 小时前
HTML与CSS基础第七课—HTML 中的a标签(跳转链接)
前端·css·html