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>
相关推荐
MavenTalk几秒前
前端技术选型之uniapp
android·前端·flutter·ios·uni-app·前端开发
雨中奔跑的小孩3 分钟前
electron打包部署vue项目
javascript·vue.js·electron
ZZZCY200315 分钟前
路由策略与路由控制实验
前端·网络
shawya_void24 分钟前
javaweb-day01-html和css初识
前端·css·html
khatung24 分钟前
React——useReducer
前端·javascript·vscode·react.js·前端框架·1024程序员节
思考的橙子27 分钟前
CSS之3D转换
前端·css·3d
木子七1 小时前
vue3-setup中使用响应式
前端·vue
廖子默1 小时前
提供html2canvas+jsPDF将HTML页面以A4纸方式导出为PDF后,内容分页时存在截断的解决思路
前端·pdf·html
Moment1 小时前
毕业半年,终于拥有了两个近 500 star 的开源项目了 🤭🤭🤭
前端·后端·开源
光影少年2 小时前
react和vue图片懒加载及实现原理
前端·vue.js·react.js