css,环形

思路:

1.先利用conic-gradient属性画一个圆,然后再叠加

效果图

javascript 复制代码
<template>
  <div class="ring">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>
<script>
import { defineComponent, reactive, toRefs, computed, onMounted } from "vue";
export default defineComponent({
  props: {
    width: {
      type: Number,
      default: 200,
    },
    height: {
      type: Number,
      default: 200,
    },
    color: {
      type: String,
      default: "#fcb844",
    },
    angle: {
      type: Number,
      default: 360,
    },
    total: {
      type: Number,
      default: 360,
    },
    unit: {
      type: String,
      default: "px",
    },
    interval: {
      type: Number,
      default: 20,
    },
    autoPlay: {
      type: Boolean,
      default: true,
    },
  },
  setup(props, { emit }) {
    const datas = reactive({
      timer: null,
      sportAge: 0,
    });
    // 设置渐变
    const bgImage = computed(() => {
      datas.sportAge = props.autoPlay ? datas.sportAge : currentAngle.value;
      return `conic-gradient(${props.color} 0deg, ${props.color} ${datas.sportAge}deg, transparent 0deg)`;
    });
    // 当前度数(deg)
    const currentAngle = computed(() => {
      return ((360 / props.total) * props.angle).toFixed(2);
    });
    // 单位前缀
    const unitSuffix = computed(() => {
      return (data, interval = 0) => {
        return data - interval + props.unit;
      };
    });
    //动画
    const animationPlay = () => {
      datas.timer = setInterval(() => {
        if (currentAngle.value <= datas.sportAge) {
          clearInterval(datas.timer);
          datas.timer = null;
          return;
        }

        datas.sportAge++;
      }, 10);
    };
    onMounted(() => {
      props.autoPlay ? animationPlay() : "";
    });
    return {
      ...toRefs(datas),
      bgImage,
      unitSuffix,
    };
  },
});
</script>
<style lang="less" scoped>
.ring {
  position: relative;
  width: v-bind(unitSuffix(width));
  height: v-bind(unitSuffix(height));
  display: flex;
  align-items: center;
  justify-content: center;
  &::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    width: v-bind(unitSuffix(width));
    height: v-bind(unitSuffix(height));
    // background-color: red; /* 对于不支持渐变的浏览器 */
    background-image: v-bind(bgImage);
    border-radius: 50%;
    transform: scale(-1, 1);
  }
  &::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: v-bind(unitSuffix(width, interval));
    height: v-bind(unitSuffix(height, interval));
    border-radius: 50%;
    background-color: #ffffff;
  }
  .content {
    position: absolute;
    z-index: 3;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
javascript 复制代码
<template>
  <div class="ring_box">
    <div class="inner">
      <ring
        :width="180"
        :height="180"
        :color="innerColor"
        :z-index="2"
        :interval="interval"
      >
      </ring>
      <div class="out">
        <ring :total="3454" :angle="2234" :interval="interval" :z-index="4">
          <ring
            :width="175"
            :height="175"
            :color="innerColor"
            :interval="interval / 2"
            :z-index="4"
          >
            <div>内容</div>
          </ring>
        </ring>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import ring from "./ring.vue";
import { defineComponent, reactive, toRefs } from "vue";
export default defineComponent({
  components: {
    ring,
  },
  setup(props) {
    const datas = reactive({
      width: 180,
      height: 180,
      outColor: "",
      innerColor: "#feedd1",
      interval: 40,
    });
    return {
      ...toRefs(datas),
    };
  },
});
</script>
<style lang="less" scoped>
.ring_box {
  width: 300px;
  height: 300px;
  display: flex;
  align-items: center;
  justify-items: center;
  position: relative;
  .out,
  .inner {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}
</style>
相关推荐
cs_dn_Jie5 分钟前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic40 分钟前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿1 小时前
webWorker基本用法
前端·javascript·vue.js
cy玩具1 小时前
点击评论详情,跳到评论页面,携带对象参数写法:
前端
清灵xmf2 小时前
TypeScript 类型进阶指南
javascript·typescript·泛型·t·infer
小白学大数据2 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
qq_390161772 小时前
防抖函数--应用场景及示例
前端·javascript
334554323 小时前
element动态表头合并表格
开发语言·javascript·ecmascript
John.liu_Test3 小时前
js下载excel示例demo
前端·javascript·excel
Yaml43 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理