自定义环形电量图uniapp可用

刚说完uniapp不可用,就惨遭打脸:注意驼峰的写法要用 kekbeb-case命名

<template>
	<view class="ring-data">
		<view class="loading">
			<view class="left" ref="left" :style="{'--background-color': backgroundColor,'--left-rote': leftRote,'--right-rote':rightRote}"></view>
			<view class="right" ref="right" :style="{'--background-color': backgroundColor,'--left-rote': leftRote,'--right-rote':rightRote}"></view>
			<view class="progress"><text style="font-size: 12px;">{{ data.percentage }}%</text></view>
		</view>
		<view class="unit"><text style="font-size: 12px;">{{ data.type }}</text></view>
	</view>
</template>

<script>
	export default {
		name: 'RingData',
		data() {
			return {
				backgroundColor: 'red',
				leftRote: 'rotateZ(180deg)',
				rightRote: 'rotateZ(180deg)'
			}
		},
		props: {
			data: {
				type: Object,
				default () {
					return {}
				},
				require: true
			}
		},
		created: function() {
			this.changeStyle()
		},
		methods: {
			changeStyle() {
				// 改变环形比例
				let rote = 0
				if (this.data.percentage < 50) {
					rote = 180 - (this.data.percentage / 100) * 360
					this.leftRote = 'rotateZ(' + rote  + 'deg)'
				} else if (this.data.percentage === 50) {
					this.leftRote = `rotateZ(0deg)`
				} else if (this.data.percentage > 50 && this.data.percentage < 100) {
					rote = Math.floor(180 - ((this.data.percentage - 50) / 100) * 360)
					this.leftRote = `rotateZ(0deg)`
					this.rightRote = 'rotateZ(' + rote + 'deg)'
				} else if (this.data.percentage === 100) {
					this.leftRote = 'rotateZ(0deg)'
					this.rightRote = 'rotateZ(0deg)'
				}
				// 改变环形颜色
				if (this.data.percentage <= 10) {
					this.backgroundColor = '#F55C5C'
				} else if (this.data.percentage > 10 && this.data.percentage <= 45) {
					this.backgroundColor = '#FF9352'
				} else if (this.data.percentage > 45 && this.data.percentage <= 70) {
					this.backgroundColor = '#6985F2'
				} else {
					this.backgroundColor = '#49D999'
				}
			}
		},
		computed: {
		}
	}
</script>

<style scoped lang="scss">
	.ring-data {
		font-size: 7px;
		display: flex;
		flex-direction: column;
		align-items: center;

		.loading {
			margin-top: 10px;
		}

		.unit {
			margin-top: 10px;
			color: #606266;
			text-align: center;
		}

		.number {
			margin-top: 10px;
			font-size: 24px;
			font-weight: bold;
			color: #1c9fe5;
			text-align: center;
		}
	}

	.loading {
		width: 8em;
		height: 8em;
		position: relative;
	}

	.loading .progress {
		position: absolute;
		width: 6em;
		height: 6em;
		font-weight: bold;
		background-color: white;
		border-radius: 50%;
		left: 1em;
		top: 1em;
		line-height: 6em;
		text-align: center;
	}

	.left,
	.right {
		width: 4em;
		height: 8em;
		overflow: hidden;
		position: relative;
		float: left;
		background-color: rgb(243, 241, 241);
	}

	.left {
		border-radius: 8em 0 0 8em;
	}

	.right {
		border-radius: 0 8em 8em 0;
	}

	.left:after,
	.right:after {
		content: '';
		position: absolute;
		display: block;
		width: 4em;
		height: 8em;
		background-color: white;
		border-radius: 8em 0 0 8em;
		background-color: var(--background-color);
	}

	.right:after {
		content: '';
		position: absolute;
		display: block;
		border-radius: 0 8em 8em 0;
	}

	.left:after {
		transform-origin: right center;
		// 0%-50%左侧
		transform: var(--left-rote);
	}

	.right:after {
		transform-origin: left center;
		// 50%-100%右侧
		transform: var(--right-rote);
	}
</style>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值