首页 > 编程语言 >JavaScript CSS Vue3 实现一个简单的Loading

JavaScript CSS Vue3 实现一个简单的Loading

时间:2024-10-26 20:33:46浏览次数:1  
标签:200 Loading const JavaScript height cursor colors let Vue3

之前项目用到的,后来换其他效果了。放博客里保存一下。

效果

视频转GIF之后不太流畅……

代码
<script setup lang="ts">

import {onBeforeUnmount, onMounted, ref} from "vue";
import {clamp} from "../scripts/Utils";

const maskDiv = ref<HTMLDivElement>(null)
const pieceNum = 14
const angleStep = 360.0 / pieceNum

const pieces = new Array<Array<HTMLDivElement>>()
const colors = new Array<number>()
const colorStep = 5
const minValue = 0
const maxValue = 200

let timer: NodeJS.Timeout | null = null
let frames: number = 0
let cursor: number = 0

const update = () => {
  if (frames % 3 === 0) {
    colors[cursor] = minValue
  }

  for (let i = 0; i < pieces.length; i++) {
    if (i === cursor) continue
    if (colors[i] < maxValue) {
      // 计算颜色
      colors[i] = clamp(colors[i] + colorStep, minValue, maxValue)
      // 应用颜色
      for (let j = 0; j < pieces[i].length; j++) {
        pieces[i][j].style.backgroundColor = `rgb(${colors[i]},${colors[i]},${colors[i]})`
      }
    }
  }

  if (frames % 3 === 0) {
    cursor = (cursor + 1) % pieceNum
  }

  frames++
}

onMounted(() => {
  const elements = document.getElementsByClassName("piece")
  for (const e of elements) {
    const children = new Array<HTMLDivElement>()
    for (const child of e.children) {
      children.push(child as HTMLDivElement)
    }
    pieces.push(children)
    colors.push(maxValue)
  }
  cursor = 0
  colors[cursor] = minValue
  timer = setInterval(update, 20)
})

onBeforeUnmount(() => {
  if (timer) {
    clearInterval(timer)
  }
})

</script>

<template>
  <div class="container">
    <div class="mask" ref="maskDiv">
      <div class="piece" v-for="i in pieceNum" :key="i" :id="`${i}`" :style="`transform: rotate(${i * angleStep}deg)`">
        <div class="circle1"></div>
        <div class="rectangle"></div>
        <div class="circle2"></div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.container {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: white;
  display: none;
  z-index: 10;
}

.mask {
  position: relative;
  width: 100%;
  height: 100%;
}

.circle1 {
  margin-bottom: -7px;
  background-color: rgb(200, 200, 200);
  border-radius: 50%;
  width: 14px;
  height: 14px;
}

.rectangle {
  width: 14px;
  height: 32px;
  background-color: rgb(200, 200, 200);
  border-radius: 10%;
}

.circle2 {
  margin-top: -7px;
  background-color: rgb(200, 200, 200);
  border-radius: 50%;
  width: 14px;
  height: 14px;
}

.piece {
  display: flex;
  flex-direction: column;
  align-items: center;
  transform-origin: center 110px;

  position: absolute;
  top: calc(50% - 100px);
  left: calc(50% - 7px);
}
</style>

标签:200,Loading,const,JavaScript,height,cursor,colors,let,Vue3
From: https://www.cnblogs.com/kui0112/p/18504457

相关文章

  • vue3使用ts和使用js
    1、使用ts<template> <divstyle="display:flex;margin:20px;"> <divstyle="width:20%;border:1pxsolidrgb(221221221);height:80vh;">1</div> <divstyle="width:60%;display:flex;justify-content:cen......
  • vue3监听和不能使用this问题,uniapp封装请求
    http.js//格式化日期函数exportfunctionrequest(method,url,data){consturls="http://183.6.96.231:29101"; constusername=uni.getStorageSync('username'); consttoken=uni.getStorageSync('token'); uni.showLoading({......
  • 大学生HTML期末大作业——HTML+CSS+JavaScript旅游网站(河南)
    HTML+CSS+JS【旅游网站】网页设计期末课程大作业web前端开发技术web课程设计网页规划与设计......
  • 大学生HTML期末大作业——HTML+CSS+JavaScript名侦探柯南
    HTML+CSS+JS【动漫网站】网页设计期末课程大作业web前端开发技术web课程设计网页规划与设计......
  • Vue3取当前时间和一个小时后的时间。
    constformatDate=(date)=>{//初始化时间   //格式化日期为YYYY-MM-DDHH:mm:ss   constyear=date.getFullYear();   constmonth=String(date.getMonth()+1).padStart(2,'0');//月份从0开始   constday=String(date.getDate())......
  • Vue3中取当前日期并且取当前日期的前10天和后30天。并把每个日期的日放到一个数组里面
    consttoday=newDate();//获取当前日期constdateArray=[];constdayArray=[];//计算前10天for(leti=10;i>0;i--){constpastDate=newDate(today);pastDate.setDate(today.getDate()-i);dateArray.push(......
  • vue3+java+springboot在线考试系统(08169)
    目录功能介绍具体实现截图技术介绍开发核心技术介绍:技术创新点vue3和vue2的区别:核心代码部分展示非功能需求分析系统开发流程软件测试源码获取功能介绍随着社会的发展,系统的管理形势越来越严峻。越来越多的用户利用互联网获得信息,但各种信息鱼龙混杂,信息真假难以辨......
  • 【Web前端】JavaScript 对象基础
     JavaScript是一种以对象为基础的编程语言,操作数据时,实际都是在处理对象。可以使用简单的数据类型(如字符串、数字和布尔值)来实现一些功能,但深入了解JavaScript对象的运作,将使你能够编写更强大和灵活的代码。对象基础JavaScript中,对象是由一组键(或属性)和值组成的无......
  • 验证码图像处理(JavaScript 版)
    图像处理在现代网页应用中扮演着重要角色,常用于图像分析和用户交互。本文将使用JavaScript和HTML5Canvas实现一些基本的图像处理操作,包括灰度转换、去除边框、提取有效区域和图像分割。环境准备首先,确保你有一个基本的HTML文件,其中包含一个Canvas元素。以下是示例结构:......
  • JavaScript 的设计优点是什么
    JavaScript作为一种广泛应用于网页开发的脚本语言,具有许多设计优点,包括:1.跨平台性;2.事件驱动和异步编程;3.灵活的语法和动态类型;4.强大的框架和库支持;5.全栈开发能力;6.开源社区和资源;7.适应性和可扩展性。JavaScript的一个主要优点是其跨平台性,这使它可在多种浏览器和操作......