微信小程序自定义组件-环形进度条

7 篇文章 0 订阅
6 篇文章 0 订阅

微信小程序自定义组件官方教程

环形进度条的组件已经放在github上

环形进度条效果图

在这里插入图片描述

创建步骤

  • 1、在根目录创建名为components的文件夹,用来放需要引用的自定义组件。
  • 2、创建名为canvas-ring的文件夹,用来放环形进度条自定义组件。
  • 3、鼠标指着canvas-ring的文件夹 鼠标右键 “新建 Component” 取名canvas-ring

结构图:
在这里插入图片描述

环形进度条组件的代码

canvas-ring.json
{
  "component": true, //这一定要写成true
  "usingComponents": {} //可以引入其他自定义组件
}
canvas-ring.wxml
<canvas style="width:{{canvasWidth}}px;height:{{canvasWidth}}px;" canvas-id="circleBar">
  <cover-view class="circle-bar-wrap" style="height:{{canvasWidth}}px;">
    <cover-view class="font">
      {{title}}
    </cover-view>
    <cover-view class="val" style="color: {{valueColor}}; margin-top:{{isMarginTop?'20':'0'}}rpx">
      {{value}} {{suffix}}
    </cover-view>
  </cover-view>
</canvas>
<slot></slot>
canvas-ring.wxss
.circle-bar-wrap{
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  box-sizing: border-box;
  padding: 0 20%;
}
.circle-bar-wrap .font{
  max-height: 62rpx;
  font-size: 26rpx;
  overflow:hidden;
  text-overflow:ellipsis;
  display:-webkit-box;
  -webkit-box-orient:vertical;
  -webkit-line-clamp:2;
  white-space: normal;
}
.circle-bar-wrap .val{
  margin-top: 20rpx;
  font-size: 50rpx;
  height: 65rpx;
}
canvas-ring.js
var windWidth = wx.getSystemInfoSync().windowWidth;
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    //画布的宽度 默认占屏幕宽度的0.4倍
    canvasWidth: {
      type: Number,
      value: windWidth * 0.4
    },
    //线条宽度 默认10
    lineWidth: {
      type: Number,
      value: 10
    },
    //线条颜色 默认"#393"
    lineColor: {
      type: String,
      value: "#393"
    },
    //标题 默认“完成率”
    title: {
      type: String,
      value: "完成率"
    },
    //当前的值 默认45
    value: {
      type: Number,
      value: 45
    },
    //值的颜色 默认"#ff9c07"
    valueColor:{
      type: String,
      value: "#ff9c07"
    },
    //最大值 默认100
    maxValue: {
      type: Number,
      value: 100
    },
    //最小值 默认0
    minValue: {
      type: Number,
      value: 0
    },
    //当前值的后缀名
    suffix: {
      type: null,
      value: "%"
    },
    //从什么角度开始 0~360之间 (12点方向为0,18点方向为180,0点方向为360)
    startDegree: {
      type: Number,
      value: 0
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    canvasWidth: windWidth * 0.4,
    isMarginTop: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showCanvasRing() {
      //去掉首位空格后如果标题为空,那么当前值的区域就没有margin-top值
      if (this.data.title.replace(/(^\s*)|(\s*$)/g, "").length == 0) {
        this.setData({
          isMarginTop: false
        })
      }
      //作画

      var ctx = wx.createCanvasContext("circleBar", this); //canvas组建封装,需要后加个this
      var circle_r = this.data.canvasWidth / 2; //画布的一半,用来找中心点和半径
      var startDegree = this.data.startDegree; //从什么角度开始
      var maxValue = this.data.maxValue; //最大值
      var minValue = this.data.minValue; //最小值
      var value = this.data.value; //当前的值
      var lineColor = this.data.lineColor; //线条颜色
      var lineWidth = this.data.lineWidth; //线条宽度
      var percent = 360 * ((value - minValue) / (maxValue - minValue)); //计算结果
      //定义起始点
      ctx.translate(circle_r, circle_r);
      //灰色圆弧
      ctx.beginPath();
      ctx.setStrokeStyle("#ebebeb");
      ctx.setLineWidth(lineWidth);
      ctx.arc(0, 0, circle_r - 10, 0, 2 * Math.PI, true);
      ctx.stroke();
      ctx.closePath();
      //有色彩的圆弧
      ctx.beginPath();
      ctx.setStrokeStyle(lineColor);
      ctx.setLineWidth(lineWidth);
      ctx.arc(0, 0, circle_r - 10, startDegree * Math.PI / 180 - 0.5 * Math.PI, percent * Math.PI / 180 + startDegree * Math.PI / 180 - 0.5 * Math.PI, false);
      ctx.stroke();
      ctx.closePath();
      ctx.draw();
    }
  }
})

使用环形进度条组件

index.json
{
  "usingComponents": {
    "canvas-ring": "/components/canvas-ring/canvas-ring"
  }
}
index.wxml
<canvas-ring id="canvasRing" value="{{c_val}}"></canvas-ring>
index.js
onReady: function() {
    var that = this;
    that.canvasRing = that.selectComponent("#canvasRing");
    that.canvasRing.showCanvasRing();
},

组件的属性介绍

属性名字类型默认值说明
canvasWidthNumber屏幕宽度的0.4倍画布宽度
titleString“完成率”标题,设置为""为空
valueNumber45当前的值
maxValueNumber100最大值
minValueNumber0最小值
suffix“%”当前值的后缀名,任何类型
lineWidthNumber10线条宽度
lineColorString“#393”线条颜色
valueColorString“#ff9c07”当前值的颜色
startDegreeNumber0从什么角度开始 0~360之间 (12点方向为0,18点方向为180,0点方向为360)

环形进度条的组件已经放在github上

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
微信小程序是一种基于微信平台的应用程序,它可以在微信中直接运行,无需下载安装。而自定义组件是小程序中的一种重要功能,它允许开发者将一些常用的UI元素封装成组件,以便在不同的页面中复用。 自定义组件具有以下特点: 1. 组件是由wxml、wxss和js文件组成,可以独立定义样式和逻辑。 2. 组件可以接受外部传入的数据,通过属性进行配置。 3. 组件可以触发事件,向外部传递消息。 4. 组件可以包含子组件,形成组件的嵌套结构。 使用自定义组件的步骤如下: 1. 在小程序项目中创建一个新的文件夹,用于存放自定义组件的相关文件。 2. 在该文件夹中创建一个wxml文件,定义组件的结构。 3. 在同一文件夹中创建一个wxss文件,定义组件的样式。 4. 在同一文件夹中创建一个js文件,定义组件的逻辑。 5. 在需要使用该组件的页面中引入组件,并在wxml中使用组件标签。 例如,我们创建一个名为"custom-component"的自定义组件,其文件结构如下: ``` custom-component/ ├── custom-component.wxml ├── custom-component.wxss └── custom-component.js ``` 在custom-component.wxml中定义组件的结构,例如: ```html <view class="custom-component"> <text>{{text}}</text> <button bindtap="handleClick">点击按钮</button> </view> ``` 在custom-component.wxss中定义组件的样式,例如: ```css .custom-component { background-color: #f5f5f5; padding: 10px; } ``` 在custom-component.js中定义组件的逻辑,例如: ```javascript Component({ properties: { text: { type: String, value: '默认文本' } }, methods: { handleClick() { this.triggerEvent('click', { message: '按钮被点击了' }); } } }) ``` 在需要使用该组件的页面中引入组件,并在wxml中使用组件标签,例如: ```html <custom-component text="Hello World" bind:click="handleCustomComponentClick"></custom-component> ``` 以上就是微信小程序自定义组件的简单介绍。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值