前端开发小技巧 - 【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>
相关推荐
漫路在线28 分钟前
JS逆向-某易云音乐下载器
开发语言·javascript·爬虫·python
不爱吃糖的程序媛31 分钟前
浅谈前端架构设计与工程化
前端·前端架构设计
BillKu2 小时前
Vue3 Element Plus 对话框加载实现
javascript·vue.js·elementui
郝YH是人间理想2 小时前
系统架构设计师案例分析题——web篇
前端·软件工程
Evaporator Core2 小时前
深入探索:Core Web Vitals 进阶优化与新兴指标
前端·windows
初遇你时动了情3 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html
QQ2740287563 小时前
Soundness Gitpod 部署教程
linux·运维·服务器·前端·chrome·web3
前端小崔3 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
哎呦你好4 小时前
HTML 表格与div深度解析区别及常见误区
前端·html
运维@小兵4 小时前
vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容
前端·javascript·vue.js