小程序自定义圆形进度环

<view class="circle-progress" style="box-shadow: inset 0 0 0 {{width}}rpx {{bgColor}}; width:{{size}}rpx ;height:{{size}}rpx" >
    <view class="circle-progress-item  circle-progress-item--l">
      <view class="circle-progress-bar" style="transform:rotate({{progressBarLVal}}deg); width:{{size}}rpx ;height:{{size}}rpx">
        <view class="huan" style="border-color:{{color}};  border-width:{{width}}rpx; "></view>
      </view>
    </view>
    <view class="circle-progress-item circle-progress-item--r">
      <view class="circle-progress-bar" style="transform:rotate({{progressBarRVal}}deg); width:{{size}}rpx ;height:{{size}}rpx">
        <view class="huan" style="border-color:{{color}};   border-width:{{width}}rpx; "></view>
      </view>
    </view>
</view>
 .circle-progress {
    width: 200rpx;
    height: 200rpx;
    position: relative;
    box-sizing: border-box;
    box-shadow: inset 0 0 0 20rpx transparent;
    border-radius: 50%;
    display: flex;
  }
  .circle-progress-item {
    width: 50%;
    height: 100%;
    overflow: hidden;
    box-sizing: border-box;
  }
  
  /* 左半环 */
  .huan {
    width: 50%;
    height: 100%;
    box-sizing: border-box;
    border-radius: 100% 0 0 100%/50%;
    border: 10px solid transparent;
    border-right: none;
  }
  
  /* 右侧半环旋转180度 */
  .circle-progress-item--r .huan {
    position: absolute;
    right: 0;
    top: 0;
    transform: rotate(180deg);
  }
  
  .circle-progress-bar {
    width: 100%;
    height: 100%;
  }
  
  .circle-progress-item--l .circle-progress-bar {
    transition: all 0.2s ease-out 0.2s;
  }
  
  .circle-progress-item--r .circle-progress-bar {
    position: relative;
    left: -102%;
    transition: all 0.2s ease-in;
  }
// pages/ring-cricle/ring-circle.js
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    progressPercent: { // 百分比
      type: Number,
      value: 80,
    },
    bgColor: { // 背景色
      type: String,
      value: '#F4F4F4'
    },
    color: { // 颜色
      type: String,
      value: '#F1DE4A'
    },
    width: { // 进度环粗细
      type: Number,
      value: 10
    },
    size: { // 进度环大小
      type: Number,
      value: 250
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    progressBarLVal: -180,
    progressBarRVal: -180,
    progressValLenght: 628,
  },

   /**
     * 组件的方法列表
     */
    methods: {
      dopercent: function(percent) {
          let progressValeR = percent;
          let progressLastLenght = this.data.progressValLenght * (percent / 100);
          let progressPercentAngle = (progressLastLenght * 360) / this.data.progressValLenght;
          console.log(progressPercentAngle,progressLastLenght)
      
          if(progressPercentAngle<180 || progressPercentAngle==180){
            this.setData({
              progressBarRVal:progressPercentAngle - 180,
              progressBarLVal:-180
            })
          }else if(progressPercentAngle > 180){
            let LastPercent = progressPercentAngle - 180;
            console.log(LastPercent - 180)
            this.setData({
              progressBarRVal:0,
              progressBarLVal:LastPercent - 180
            })
          }
          // 角度 = 弧长×360°÷(2×半径×π)
          // 弧长 =角度×(2×半径×π)÷ 360°
      },
      reZero: function() {
        this.dopercent(0)
      },
      doAnimation: function() {
        this.dopercent(this.data.progressPercent)
      },

    },
    pageLifetimes: {
      show: function() {
      this.dopercent(this.data.progressPercent)
    },
    }
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值