前端开发小技巧 - 【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>
相关推荐
柳杉19 分钟前
震惊!字符串还能这么玩!
前端·javascript
是上好佳佳佳呀1 小时前
【前端(五)】CSS 知识梳理:浮动与定位
前端·css
仍然.1 小时前
算法题目---模拟
java·javascript·算法
wefly20171 小时前
纯前端架构深度解析:jsontop.cn,JSON 格式化与全栈开发效率平台
java·前端·python·架构·正则表达式·json·php
我命由我123453 小时前
React - 类组件 setState 的 2 种写法、LazyLoad、useState
前端·javascript·react.js·html·ecmascript·html5·js
聊聊MES那点事3 小时前
JavaScript图表控件AG Charts使用教程:使用AG Charts React实时更新柱状图
开发语言·javascript·react.js·图表控件
自由生长20243 小时前
IndexedDB的观察
前端
IT_陈寒4 小时前
Vite热更新坑了我三天,原来配置要这么写
前端·人工智能·后端
斯班奇的好朋友阿法法4 小时前
离线ollama导入Qwen3.5-9B.Q8_0.gguf模型
开发语言·前端·javascript
掘金一周4 小时前
每月固定续订,但是token根本不够用,掘友们有无算力焦虑啊 | 沸点周刊 4.2
前端·aigc·openai