微信小程序进度条详解 progress 自定圆形进度条

水深无声 2023-10-07 17:32 51阅读 0赞

也许你迷茫,但是我想说,在你迷茫的同时,保持本心,过好今天就好。

在微信小程序开发中,progress用来实现水平进度条效果

在这里插入图片描述

1 基本使用
  1. <progress percent="80" stroke-width="12" show-info color="pink" active />

效果就是如上图所示

  • percent 表示当前的进度
  • stroke-width 进度条的宽度
  • color 进度样的颜色
  • show-info 最右侧显示当前进度
  • active 如果所示的动态加载显示
2 自定圆形进度条

在这里插入图片描述

2.1 wxml
  1. <view class="container">
  2. <view class='progress_box'>
  3. <!-- 背景 灰色 -->
  4. <canvas class="progress_bg" id="canvasProgressbg" canvas-id="canvasProgressbg"> </canvas>
  5. <!-- 进度 -->
  6. <canvas class="progress_canvas" id="canvasProgress" canvas-id="canvasProgress"> </canvas>
  7. <!-- 中间显示的文本 -->
  8. <view class="progress_text">
  9. <view class="progress_dot"></view>
  10. <text class='progress_info'> {
  11. {
  12. progress_txt}}</text>
  13. </view>
  14. </view>
  15. </view>
2.2 js
  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. progress_txt: '加载中...',
  7. count: 0, // 设置 计数器 初始为0
  8. countTimer: null // 设置 定时器 初始为null
  9. },
  10. //倒计时方法
  11. countInterval: function () {
  12. // 设置倒计时 定时器 每100毫秒执行一次,计数器count+1 ,耗时6秒绘一圈
  13. this.countTimer = setInterval(() => {
  14. if (this.data.count <= 60) {
  15. /* 绘制彩色圆环进度条
  16. 注意此处 传参 step 取值范围是0到2,
  17. 所以 计数器 最大值 60 对应 2 做处理,计数器count=60的时候step=2
  18. */
  19. this.drawProgressCircle(this.data.count / (60 / 2));
  20. this.setData({
  21. progress_txt: (this.data.count++) + '%'
  22. });
  23. } else {
  24. this.setData({
  25. progress_txt: "加载完成"
  26. });
  27. clearInterval(this.countTimer);
  28. }
  29. }, 100)
  30. },
  31. /**
  32. * 绘制灰色背景
  33. */
  34. drawProgressbg: function () {
  35. // 使用 wx.createContext 获取绘图上下文 context
  36. var ctx = null;
  37. wx.createSelectorQuery()
  38. .select("#canvasProgressbg")
  39. .context(function (res) {
  40. console.log("节点实例:", res);
  41. // 节点对应的 Canvas 实例。
  42. ctx = res.context;
  43. ctx.setLineWidth(4); // 设置圆环的宽度
  44. ctx.setStrokeStyle('#EEEEEE'); // 设置圆环的颜色
  45. ctx.setLineCap('round') // 设置圆环端点的形状
  46. ctx.beginPath(); //开始一个新的路径
  47. ctx.arc(110, 110, 100, 0, 2 * Math.PI, false);
  48. //设置一个原点(110,110),半径为100的圆的路径到当前路径
  49. ctx.stroke(); //对当前路径进行描边
  50. ctx.draw();
  51. })
  52. .exec();
  53. },
  54. /**
  55. * 绘制小程序进度
  56. * @param {*} step
  57. */
  58. drawProgressCircle: function (step) {
  59. let ctx = null;
  60. wx.createSelectorQuery()
  61. .select("#canvasProgress")
  62. .context(function (res) {
  63. console.log("节点实例:", res); // 节点对应的 Canvas 实例。
  64. ctx = res.context;
  65. // 设置渐变
  66. var gradient = ctx.createLinearGradient(200, 100, 100, 200);
  67. gradient.addColorStop("0", "#2661DD");
  68. gradient.addColorStop("0.5", "#40ED94");
  69. gradient.addColorStop("1.0", "#5956CC");
  70. ctx.setLineWidth(10);
  71. ctx.setStrokeStyle(gradient);
  72. ctx.setLineCap('round')
  73. ctx.beginPath();
  74. // 参数step 为绘制的圆环周长,从0到2为一周 。 -Math.PI / 2 将起始角设在12点钟位置 ,结束角 通过改变 step 的值确定
  75. ctx.arc(110, 110, 100, -Math.PI / 2, step * Math.PI - Math.PI / 2, false);
  76. ctx.stroke();
  77. ctx.draw()
  78. })
  79. .exec();
  80. },
  81. onReady: function () {
  82. this.drawProgressbg();
  83. this.countInterval()
  84. },
  85. })
2.3 wxss 样式文件中
  1. .progress_box{
  2. position: relative;
  3. width:220px;
  4. height: 220px;
  5. display: flex;
  6. align-items: center;
  7. justify-content: center;
  8. }
  9. .progress_bg{
  10. position: absolute;
  11. width:220px;
  12. height: 220px;
  13. }
  14. .progress_canvas{
  15. width:220px;
  16. height: 220px;
  17. }
  18. .progress_text{
  19. position: absolute;
  20. display: flex;
  21. align-items: center;
  22. justify-content: center
  23. }
  24. .progress_info{
  25. font-size: 36rpx;
  26. padding-left: 16rpx;
  27. letter-spacing: 2rpx
  28. }
  29. .progress_dot{
  30. width:16rpx;
  31. height: 16rpx;
  32. border-radius: 50%;
  33. background-color: #fb9126;
  34. }

完毕

发表评论

表情:
评论列表 (有 0 条评论,51人围观)

还没有评论,来说两句吧...

相关阅读