肥胖-超重–正常–偏瘦,四个区间,用渐变色柱体显示,小三角标记位置。
根据接口返回的值,小三角移动到不同的区间不同的位置。
百分比显示;
<template>
<div class="box">
<div class="bmi-show">
<div class="triangle" :style="{ bottom: percentage }"></div>
<div class="linearShow"></div>
<div class="range">
<p>肥胖</p>
<p>超重</p>
<p>正常</p>
<p>偏瘦</p>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
bmi: "",
percentage: ""
};
},
// 项目中使用的是computed, this.bmi是从接口拿到的数据。
// 现在单页面,写到watch里面,是因为bmi的值在computed中不能改变,不然发生报错;
watch: {
percentage() {
console.log(this.bmi);
if (!this.bmi) return "";
if (this.bmi < 14.5) this.bmi = 14.5;
if (this.bmi > 33.5) this.bmi = 33.5;
let diff = this.bmi - 14.5;
return `calc(${(diff * 100) / 19}% - 4px)`;
}
}
};
</script>
<style lang="scss" scoped>
.box {
position: relative;
}
.bmi-show {
position: absolute;
right: 40px;
top: 50px;
display: flex;
.triangle {
width: 0;
height: 0;
border-top: 4px solid transparent;
border-left: 4px solid #000;
border-bottom: 4px solid transparent;
position: absolute;
left: -10px;
}
.linearShow {
width: 10px;
height: 170px;
border-radius: 8px;
background: linear-gradient(
360deg,
#00d5ed 0%,
#7aec01 28%,
#ffd500 67%,
#f74d00
);
}
.range {
margin-left: 10px;
font-size: 16px;
color: #000;
line-height: 33px;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
}
}
</style>