当前位置: 首页 > news >正文

[Uni-app] 微信小程序的圆环进度条

效果图:

组件完整代码如下:

<template><view class="base-style":style="'position: relative;width: ' + diameter + 'px;height: ' + diameter + 'px;display: flex;flex-direction: row;background-color: ' + bgColor + ';'"><!-- 左半圆和右半圆都要经历下面的5步:[第1步]第1层限定区域; [第2步]第2层决定显示一个整圆的左半边还是右半边; [第3步]第3层先使用激活颜色绘制一个圆环, 再添加一个同级且宽度为区域一半的盒子A;[第4步]在盒子A中再使用圆环底色绘制一个圆环, 此时整个圆环是 '左一半是激活颜色、右一半是圆环底色', 但这个圆环同时只能被看到一半;[第5步]旋转第2层。 --><!-- 左半圆 --><view class="base-style" :style="firstLayerViewStyle"><view :style="secondLayerViewStyle + secondLayerForLeft"><!-- 使用激活颜色绘制一个圆环。 --><view :style="thirdLayerStyle"></view><!-- 再使用背景色遮盖同级圆环的一半。 --><view class="base-style" :style="thirdLayerStyleForBg"><view :style="fourthLayerStyleForBg" /></view><view v-if="0 < ePercent && ePercent < 0.5" :style="endPointStyle + endPointStyleForLeft" /></view></view><!-- 右半圆 --><view class="base-style" :style="firstLayerViewStyle"><!-- 适配:为了避免右侧遮盖显示不全 此处向左多移动了1px --><view :style="secondLayerViewStyle + 'left: ' + (- diameter / 2 - 1) + 'px;' + secondLayerForRight"><!-- 使用激活颜色绘制一个圆环。 --><view :style="thirdLayerStyle"></view><!-- 再使用背景色遮盖同级圆环的一半。 --><view class="base-style" :style="thirdLayerStyleForBg"><view :style="fourthLayerStyleForBg" /></view><view v-if="ePercent > 0.5" :style="endPointStyle + endPointStyleForRight" /></view></view><view v-if="0.5 == ePercent" :style="endPointStyle + 'background-color: ' + this.hoopBgColor + ';'" /><!-- #ifdef APP-PLUS --><!-- 处理现象: 安卓App的顶部和底部会有一个小白点。 --><!-- <view v-if="ePercent > 0.5" :style="'position: absolute;top: 0;' + repairPointStyle" /> --><!-- <view v-if="1.0 == ePercent" :style="'position: absolute;bottom: 0;' + repairPointStyle" /> --><!-- #endif --></view>
</template><!-- 组件名称: 圆环进度条。启发地址: https://www.cnblogs.com/jr1993/p/4677921.html 。编者信息: 867003077@qq.com 。 -->
<script>export default {name: 'progressCircle',props: {// 背景色(不宜设置为透明 否则 需要 在 左thirdLayer 的外面 再嵌套一个盒子)。bgColor: {type: String,default: '#FFFFFF'},// 圆环的外直径(单位px)。diameter: {type: Number,default: 250},// 圆环线条的厚度(单位px)。hoopThickness: {type: Number,default: 8},// 圆环底色(灰色的圆环)。hoopBgColor: {type: String,// default: 'transparent'default: '#F3F3F3'},// 圆环激活部分的颜色。hoopColor: {type: String,default: '#FF4C20'},// 圆环进度百分比值(其值范围在0到1之间)。percent: {type: [Number, String],default: 0,validator: val => {return val >= 0 && val <= 1;},},animate: {type: Boolean,default: false,},},data() {return {targetPercent: 0,ePercent: 0,showTimer: undefined,};},watch: {percent: {handler: function() {console.log('progressCircle_watch_percent', this.percent);this.loadData();},},},computed: {firstLayerViewStyle() {return 'position: relative;width: ' + (this.diameter / 2) +'px;height: ' + this.diameter + 'px;';},secondLayerViewStyle() {return 'box-sizing: border-box;position: absolute;top: 0;width: ' + this.diameter +'px;height: ' + this.diameter + 'px;';},thirdLayerStyle() {return 'box-sizing: border-box;width: ' + this.diameter + 'px;height: ' + this.diameter +'px;border-radius: ' + (this.diameter / 2) +'px;border-width: ' + this.hoopThickness +'px;border-style: solid;border-color: ' + this.hoopColor + ';';},thirdLayerStyleForBg() {return 'box-sizing: border-box;position: absolute;top: 0;left: ' + (this.diameter / 2) + 'px;width: ' +this.diameter + 'px;height: ' + this.diameter + 'px;background-color: ' + this.bgColor + ';';},fourthLayerStyleForBg() {return 'box-sizing: border-box;margin-left: ' + (-this.diameter / 2) + 'px;width: ' + this.diameter +'px;height: ' +this.diameter + 'px;border-radius: ' + (this.diameter / 2) + 'px;border-width: ' +this.hoopThickness + 'px;border-style: solid;border-color: ' + this.hoopBgColor + ';';},secondLayerForLeft() {let angle = 0;if (this.ePercent < 0.5) {angle += (180 * (this.ePercent - 0.5) / 0.5);}// #ifdef APP-PLUSreturn 'left: 0;transform: rotate(' + angle + 'deg);';// #endif// #ifdef MP-WEIXINreturn 'left: 0;transform: rotate(' + angle + 'deg);-webkit-transform: rotate(' + angle + 'deg);';// #endif},secondLayerForRight() {let angle = 0;if (this.ePercent > 0.5) {angle += (180 * (this.ePercent - 0.5) / 0.5);}// #ifdef APP-PLUSreturn 'right: 0;transform: rotate(' + angle + 'deg);';// #endif// #ifdef MP-WEIXINreturn 'right: 0;transform: rotate(' + angle + 'deg);-webkit-transform: rotate(' + angle + 'deg);';// #endif},// repairPointStyle() {// 	return 'left: ' + (this.diameter - this.hoopThickness) / 2 + 'px;width: ' +// 		this.hoopThickness + 'px;height: ' + this.hoopThickness + 'px;border-radius: ' +// 		this.hoopThickness / 2 + 'px;background-color: ' + this.hoopColor + ';';// },endPointStyle() {// 结束点圆心圈直径。const _circleCenterRadius = 2;return 'box-sizing: border-box;position: absolute;top: 0;left: ' + (this.diameter - this.hoopThickness) / 2 +'px;width: ' +this.hoopThickness + 'px;height: ' + this.hoopThickness + 'px;border-radius: ' + (this.hoopThickness / 2) +'px;border-width: ' + (this.hoopThickness / 2 - _circleCenterRadius) +'px;border-style: solid;border-color: ' +this.hoopColor + ';';},endPointStyleForLeft() {return 'background-color: ' + ((this.ePercent > 0.5) ? this.hoopColor : this.hoopBgColor) + ';';},endPointStyleForRight() {return 'background-color: ' + ((1 == this.ePercent) ? this.hoopColor : this.hoopBgColor) + ';';},},mounted() {console.log('progressCircle_mounted');this.loadData();},methods: {loadData() {this.targetPercent = parseFloat(this.percent);console.log('progressCircle_loadData');if (!this.animate) {this.ePercent = this.targetPercent;} else {let _this = this;this.ePercent = 0;this.showTimer && clearInterval(this.showTimer);this.showTimer = setInterval(() => {let tempPercent = _this.ePercent + 0.1;if (tempPercent < _this.targetPercent) {_this.ePercent = tempPercent;return;};_this.ePercent = _this.targetPercent;clearInterval(_this.showTimer);}, 200);}}}}
</script><style scoped>.base-style {box-sizing: border-box;/* 溢出隐藏 */overflow: hidden;}
</style>

调用页面:

<template><view class="my-page-container" :style="{ 'height': pageBoxH + 'px' }" @click="currentPercent=0.8"><progress-circle class="mine-member-level-progress" :diameter="180" :hoopThickness="10" :hoopColor="'orange'":percent="currentPercent" :animate="true" /></view>
</template><script>/** 演示页面 */import progressCircle from "@/components/progress-circle/index.vue";// import {//   queryDetail,// } from '@/api/mine.js';export default {name: 'myDemo',components: {progressCircle,},data() {return {pageBoxH: 1000,currentPercent: 0.25,};},beforeCreate() {console.log('beforeCreate enter');},created() {console.log('created enter');},mounted() {console.log('mounted enter');},onLoad(option) {console.log('onLoad enter');},onReady() {},methods: {},}
</script><style scoped>.my-page-container {background-color: white;box-sizing: border-box;padding: 10px 10px 50px 10px;display: flex;flex-direction: column;}
</style>

相关文章:

  • windows安装ssh
  • 什么是Java内存模型|Java内存模型的核心概念是什么
  • 设计模式 适配器模式
  • ubuntu10.04 apache2.2开启tls1.2的支持,使现代的edge和firefox浏览器能正常访问https
  • Debezium日常分享系列之:Debezium2.5稳定版本之处理常见问题
  • Spring Boot从入门到实战
  • linux源配置:ubuntu、centos;lspci与lsmod命令区别
  • java中获取字符串中满足正则表达式的元素集合
  • 【RabbitMQ | 第四篇】基于RabbitMQ实现延迟队列
  • 《模板入门》以及初始STL
  • JAVA EE (计算机是如何工作的)
  • 【STM32】读写BKP备份寄存器RTC实时时钟
  • 《论文阅读》带边界调整的联合约束学习用于情感原因对提取 ACL 2023
  • android 音频焦点,音频策略梳理
  • 冗余双写方案下数据一致性问题解决及延申问题处理方案
  • [PHP内核探索]PHP中的哈希表
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • Apache的80端口被占用以及访问时报错403
  • Brief introduction of how to 'Call, Apply and Bind'
  • CSS实用技巧干货
  • es6要点
  • HTML5新特性总结
  • Python 基础起步 (十) 什么叫函数?
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 对象引论
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 模型微调
  • 删除表内多余的重复数据
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 延迟脚本的方式
  • 译米田引理
  • 白色的风信子
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • #100天计划# 2013年9月29日
  • (4)logging(日志模块)
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (Ruby)Ubuntu12.04安装Rails环境
  • (办公)springboot配置aop处理请求.
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)四层和七层负载均衡的区别
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .Net IOC框架入门之一 Unity
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET 中的轻量级线程安全
  • .NET/C# 使窗口永不获得焦点
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • @angular/cli项目构建--http(2)
  • @modelattribute注解用postman测试怎么传参_接口测试之问题挖掘
  • [ C++ ] STL---仿函数与priority_queue
  • [ 云计算 | AWS 实践 ] Java 如何重命名 Amazon S3 中的文件和文件夹
  • [20170713] 无法访问SQL Server