带图标的Loading组件封装

效果如下:

在这里插入图片描述

代码如下:
<template>
  <div class="loading-wrapper">
    <div class="loading-container">
      <!-- 外层灰色滑轨 -->
      <svg class="loading-svg" viewBox="0 0 100 100">
        <circle
          class="track"
          cx="50"
          cy="50"
          r="47"
          stroke-width="6"
          fill="none"
        />
        <!-- 蓝色旋转滑块 -->
        <circle
          class="progress"
          cx="50"
          cy="50"
          r="47"
          stroke-width="6"
          stroke-linecap="round"
          fill="none"
        />
      </svg>
      
      <!-- 中间固定图标 -->
      <div class="center-icon">
        <slot name="icon">
          <!-- 默认图标 -->
          <svg viewBox="0 0 24 24" width="32" height="32" fill="#2E83FF">
            <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/>
          </svg>
        </slot>
      </div>
    </div>
    <!-- 可选的文字提示 -->
    <div v-if="text" class="loading-text">{{ text }}</div>
  </div>
</template>

<script setup>
import { ref, defineProps } from 'vue';

const props = defineProps({
  text: {
    type: String,
    default: ''
  },
  size: {
    type: Number,
    default: 80
  },
  color: {
    type: String,
    default: '#2E83FF' // 蓝色滑块
  }
});
</script>

<style lang="scss" scoped>
.loading-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.loading-container {
  position: relative;
  width: v-bind(size + 'px');
  height: v-bind(size + 'px');
}

.loading-svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg); /* 从顶部开始 */
}

/* 灰色滑轨 */
.track {
  stroke: #e0e0e0;
}

/* 蓝色旋转滑块 */
.progress {
  stroke: v-bind(color);
  stroke-dasharray: 295; /* 2 * PI * 47 = 295.31 */
  stroke-dashoffset: 70;  /* 留出缺口 */
  animation: rotate 1.5s linear infinite;
  transform-origin: center;
}

@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 中间固定图标 */
.center-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: v-bind(color);
  /* 图标不旋转 */
}

.loading-text {
  margin-top: 16px;
  font-size: 14px;
  color: #606266;
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝莓味的口香糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值