带图标的Loading组件封装

效果如下:
代码如下:
javascript 复制代码
<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>
相关推荐
互联网中的一颗神经元11 小时前
小白python入门 - 39. 采集流水线小项目
开发语言·python
徐小夕12 小时前
花了一周,3亿tokens,我开源了一款 Word 文档智能审查平台,文稿自动质检+可视化分析,告别低效人工审核
前端·算法·github
Wang's Blog12 小时前
Go-Zero 项目开发22:用户群聊功能的实现与完善
开发语言·golang
a11177612 小时前
坦克大战3D Three.js 3D (开源项目)
开发语言·javascript·3d
kyriewen13 小时前
别再乱用useEffect了——你写的10个里有8个不该存在
前端·javascript·react.js
Ivanqhz13 小时前
Rust &‘static str浅析
java·前端·javascript·rust
IT_陈寒14 小时前
SpringBoot这个分页坑,我踩了三天才爬出来
前端·人工智能·后端
颜酱14 小时前
05 | 召回前置准备:根据业务数据库生成各数据库(读取配置阶段)
前端·人工智能·后端
Wang's Blog14 小时前
Go-Zero项目开发24: 基于Bitmap实现群聊消息已读未读
开发语言·后端·golang
zandy101114 小时前
衡石 Agentic BI的ReAct 推理框架在 Agentic BI 中的工程化实践
前端·javascript·react.js