• 环形进度条组件


    在这里插入图片描述

    <template>
      <div class="content" ref="box">
        <svg
          :id="idStr"
          style="transform: rotate(-90deg)"
          :width="width"
          :height="width"
          xmlns="http://www.w3.org/2000/svg"
        >
          <linearGradient :id="`gradient-${id}`" gradientUnits="userSpaceOnUse">
            <stop
              v-for="(item, index) in gradientsColor"
              :key="index"
              :offset="item.offset"
              :stop-color="item.color"
            />
          linearGradient>
          <circle
            :r="(width-radius)/2"
            :cy="width/2"
            :cx="width/2"
            :stroke-width="radius"
            :stroke="backgroundColor"
            fill="none"
          />
          <circle
            v-if="gradientsColorShow"
            ref="$bar"
            :r="(width-radius)/2"
            :cy="width/2"
            :cx="width/2"
            :stroke="gradientsColor ? `url(#gradient-${id})` : barColor"
            :stroke-width="radius"
            :stroke-linecap="isRound ? 'round' : 'square'"
            :stroke-dasharray="(width-radius)*3.14"
            :stroke-dashoffset="dashoffsetValue"
            fill="none"
          />
        svg>
        <div class="center_text">
          <p v-if="!$slots.default" class="title">{{progress}}%p>
          <slot>slot>
        div>
      div>
    template>
    
    <script>
    // import _ from "lodash";
    export default {
      props: {
        widthPresent: {
          type: Number,
          default: 1
        },
        gradientsColor: {
          type: [Boolean, Array],
          default: false
        },
        radius: {
          type: [Number, String],
          default: 20
        }, // 进度条厚度
        progress: {
          type: [Number, String],
          default: 20
        }, // 进度条百分比
        barColor: {
          type: String,
          default: "#1890ff"
        }, // 进度条颜色
        backgroundColor: {
          type: String,
          default: "rgba(0,0,0,0.3)"
        }, // 背景颜色
        isAnimation: {
          // 是否是动画效果
          type: Boolean,
          default: true
        },
        isRound: {
          // 是否是圆形画笔
          type: Boolean,
          default: true
        },
        id: {
          // 组件的id,多组件共存时使用
          type: [String, Number],
          default: 1
        },
        duration: {
          // 整个动画时长
          type: [String, Number],
          default: 1000
        },
        delay: {
          // 延迟多久执行
          type: [String, Number],
          default: 200
        },
        timeFunction: {
          // 动画缓动函数
          type: String,
          default: "cubic-bezier(0.99, 0.01, 0.22, 0.94)"
        }
      },
      data() {
        return {
          width: 200,
          idStr: "",
          oldValue: 0
        };
      },
      computed: {
        gradientsColorShow: function() {
          return true;
        },
        dashoffsetValue: function() {
          const { isAnimation, width, radius, progress, oldValue } = this;
          return isAnimation
            ? ((width - radius) * 3.14 * (100 - oldValue)) / 100
            : ((width - radius) * 3.14 * (100 - progress)) / 100;
        }
      },
      watch: {
        id: function() {
          this.idStr = `circle_progress_keyframes_${this.id || 1}`;
        },
        progress: function(newData, oldData) {
          if (newData !== oldData) {
            this.oldValue = oldData;
            this.setAnimation();
          }
        }
      },
      mounted() {
        this.idStr = `circle_progress_keyframes_${this.id || 1}`;
        let self = this;
        this.setCircleWidth();
        this.setAnimation();
        // 此处不能使用window.onresize
        window.addEventListener("resize", function() {
          self.setCircleWidth();
          self.setAnimation(self);
        });
      },
      methods: {
        setCircleWidth() {
          const { widthPresent } = this;
          this.$nextTick(() => {
            let box = this.$refs.box;
            let width = box.clientWidth * widthPresent;
            let height = box.clientHeight * widthPresent;
            let cW = width > height ? height : width;
            this.width = cW;
          })
        },
        setAnimation() {
          let self = this;
          if (self.isAnimation) {
            // 重复定义判断
            if (document.getElementById(self.idStr)) {
              self.$refs.$bar.classList.remove(`circle_progress_bar${self.id}`);
            }
            this.$nextTick(() => {
              this.startAnimation();
            });
          }
        },
        startAnimation() {
          // 生成动画样式文件
          let style = document.createElement("style");
          style.id = this.idStr;
          style.type = "text/css";
          style.innerHTML = `
          @keyframes circle_progress_keyframes_name_${this.id} {
          from {stroke-dashoffset: ${((this.width - this.radius) *
            3.14 *
            (100 - this.oldValue)) /
            100}px;}
          to {stroke-dashoffset: ${((this.width - this.radius) *
            3.14 *
            (100 - this.progress)) /
            100}px;}}
          .circle_progress_bar${
            this.id
          } {animation: circle_progress_keyframes_name_${this.id} ${
            this.duration
          }ms ${this.delay}ms ${this.timeFunction} forwards;}`;
          // 添加新样式文件
          document.getElementsByTagName("head")[0].appendChild(style);
          // 往svg元素中添加动画class
          this.$refs.$bar.classList.add(`circle_progress_bar${this.id}`);
        }
      }
    };
    script>
    <style  scoped>
    .content {
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .center_text {
      position: absolute;
      font-size: 16px;
      font-weight: bold;
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
  • 相关阅读:
    记一次http接口自动重试现象的排查
    基于ssm的智慧家政在线预约管理系统的设计与实现-计算机毕业设计源码
    comfyui安装指南及animaldiff使用
    【向量数据库】相似向量检索Faiss数据库的安装及余弦相似度计算(C++)
    计算机二级WPS 选择题(模拟和解析八)
    YOLOv5改进Neck系列:新颖的Gather-and-Distribute机制,特征新颖融合,增强了多尺度特征融合能力,实现了延迟和准确性的理想平衡
    斗地主 功能测试实战--需求分析,欢乐豆到底怎么输光的
    记录crack某IDE插件过程
    VBA 输出到CMD控制台显示暨更新当前行显示
    窗口函数实战指南:轻松掌握排名计算技巧,提升数据处理效率
  • 原文地址:https://blog.csdn.net/lzl980111/article/details/126056041