前端开发小技巧 - 【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>
相关推荐
Senar23 分钟前
Web端选择本地文件的几种方式
前端·javascript·html
烛阴41 分钟前
UV Coordinates & Uniforms -- OpenGL UV坐标和Uniform变量
前端·webgl
姑苏洛言1 小时前
扫码小程序实现仓库进销存管理中遇到的问题 setStorageSync 存储大小限制错误解决方案
前端·后端
烛阴1 小时前
JavaScript 的 8 大“阴间陷阱”,你绝对踩过!99% 程序员崩溃瞬间
前端·javascript·面试
lh_12541 小时前
ECharts 地图开发入门
前端·javascript·echarts
jjw_zyfx1 小时前
成熟的前端vue vite websocket,Django后端实现方案包含主动断开websocket连接的实现
前端·vue.js·websocket
Mikey_n2 小时前
前台调用接口的方式及速率对比
前端
周之鸥2 小时前
使用 Electron 打包可执行文件和资源:完整实战教程
前端·javascript·electron
我爱吃朱肉2 小时前
HTMLCSS模板实现水滴动画效果
前端·css·css3
机器视觉知识推荐、就业指导2 小时前
开源QML控件:进度条滑动控件(含源码下载链接)
前端·qt·开源·qml