国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 小程序 > 正文

微信小程序自定義組件實現環形進度條

2020-03-21 15:53:14
字體:
來源:轉載
供稿:網友

本文實例為大家分享了微信小程序實現環形進度條的具體代碼,供大家參考,具體內容如下

微信小程序自定義組件官方教程

環形進度條的組件已經放在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();},

組件的屬性介紹

微信小程序,組件,環形進度條

環形進度條的組件已經放在github

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昌都县| 郎溪县| 东方市| 平谷区| 忻州市| 肃南| 娄烦县| 铜川市| 大石桥市| 共和县| 泉州市| 积石山| 错那县| 乾安县| 商河县| 龙南县| 冕宁县| 泉州市| 静宁县| 旺苍县| 蕲春县| 综艺| 宜良县| 辽源市| 永胜县| 从江县| 南江县| 中卫市| 涡阳县| 湘潭市| 龙州县| 汉阴县| 阿勒泰市| 麻城市| 周宁县| 宁德市| 淮安市| 石景山区| 长泰县| 谷城县| 固原市|