带图标的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>
相关推荐
Brilliantwxx17 小时前
【C++】 手撕 AVLTree :从零实现自平衡二叉搜索树
开发语言·c++·笔记·算法
牛奶17 小时前
从卡顿到顺滑,只差这几个优化
前端·chrome·浏览器
qq_4107321717 小时前
嵌入式开发-memcpy与memmove 技术详解
java·linux·开发语言·嵌入式硬件
卸任17 小时前
打造基于 Milkdown 的所见即所得 Markdown 编辑器
前端·react.js·markdown
夜雪闻竹17 小时前
Electron 入门:Web 应用打包成桌面软件
前端·javascript·electron
勇哥的编程江湖17 小时前
25 Elasticsearch Terms Aggregation 实战
java·服务器·前端
不吃土豆的马铃薯17 小时前
网络 IO 核心(同步/异步)概念笔记
服务器·c语言·开发语言·网络·c++·笔记
张小凡vip17 小时前
python的__init__.py说明
开发语言·前端·python
努力努力再努力wz17 小时前
【Redis入门系列】:从 hashtable到 listpack:深入理解 Hash 底层编码、字段级过期、核心命令与缓存应用
开发语言·数据结构·数据库·c++·redis·算法·缓存