带图标的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>
相关推荐
微信开发api-视频号协议6 分钟前
企业微信外部群开发自动化实践过程
java·前端·微信·自动化·企业微信·ipad
甲维斯11 分钟前
Fable5:20美金的顶级设计师!
前端·人工智能
kyriewen27 分钟前
我同时用了 Claude Code、Cursor 和 Codex,说说三个工具的真实差距——2026 年选错工具比不用 AI 更浪费时间
前端·ai编程·claude
必须得开心呀1 小时前
QT解决中文乱码问题
开发语言·qt
Jackson__1 小时前
为了拯救我的腰椎和颈椎,我做了款浏览器插件!
前端·javascript·开源
闲猫1 小时前
Python FastAPI + SQLAlchemy 入门教程:从零搭建你的第一个 Web 应用
前端·python·fastapi
林小帅1 小时前
NestJS v11 + Prisma v7 ESM 迁移配置解析
前端·javascript·后端
IT_陈寒2 小时前
React的setState竟然不是立刻生效的,害我调试半天
前端·人工智能·后端
这不小天嘛2 小时前
JAVA八股——redis篇
java·开发语言·redis
简简单单就是我_hehe2 小时前
前端埋点日志解决方案:完整 User-Agent 解析工具实现,爬虫识别、设备品牌、操作系统、浏览器版本全解析(附完整源码)
前端·爬虫