封装一个简单的loading组件

76 阅读1分钟
<template>
  <div class="loading-container">
    <div class="loading-spin" :class="size">
      <template v-for="(item, index) in Array.from({ length: 4 })" :key="index">
        <div :class="size"></div>
      </template>
    </div>
    <div class="loading-text" :class="size">{{ text }}</div>
  </div>
</template>
<script setup>
import { defineProps } from "@vue/runtime-core";

defineProps({
  text: {
    type: String,
    default: "加载中",
  },
  size: {
    type: String,
    default: "small",
  },
});
</script>
<style lang="less">
.loading-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.loading-spin {
  display: inline-block;
  position: relative;
  width: 20px;
  height: 20px;

  &.large {
    width: 24px;
    height: 24px;
  }
  &.meduim {
    width: 20px;
    height: 20px;
  }
  &.small {
    width: 16px;
    height: 16px;
  }

  div {
    position: absolute;
    width: 20px;
    height: 20px;
    background: blue;
    border-radius: 50%;
    animation: loading 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;

    &.large {
      width: 30px;
      height: 30px;
    }
    &.meduim {
      width: 24px;
      height: 24px;
    }
    &.small {
      width: 16px;
      height: 16px;
    }
  }

  div:nth-child(1) {
    animation-delay: -0.45s;
  }
  div:nth-child(2) {
    animation-delay: -0.3s;
  }
  div:nth-child(3) {
    animation-delay: -0.15s;
  }

  @keyframes loading {
    0% {
      transform: scale(0);
    }
    100% {
      transform: scale(1);
      opacity: 0;
    }
  }
}

.loading-text {
  font-size: 16px;
  font-weight: bold;
  color: blue;

  &.large {
    font-size: 18px;
  }
  &.meduim {
    font-size: 16px;
  }
  &.small {
    font-size: 14px;
  }
}
</style>