Vue2电池小图标

简单的电池小图标,根据电量展示不同的颜色。

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<template>
<div class="battery-container">
<div class="shell">
<div
v-for="(item,index) in batteryChunk"
class="block"
:key="index"
:style="{
background: `${batteryColor}`
}"
></div>
</div>
</div>
</template>

<script>
export default {
props:{
capacity:{
type: [Number,String],
}
},
computed:{
batteryChunk(){
return this.findInterval(this.capacity)
},
batteryColor(){
if(this.batteryChunk >=0 && this.batteryChunk <= 1){
return '#FE5F69'
}else if(this.batteryChunk > 1 && this.batteryChunk <=4){
return '#FFC95C'
}else{
return '#64BA8C'
}
}
},
methods:{
findInterval(num) {
var intervalSize = 100 / 6
var interval = Math.floor(num / intervalSize)
if (interval === 6) {
return interval
} else {
return interval + 1
}
}
}
}
</script>
<style lang='less' scoped>
.battery-container{
width: 48px;
height: 24px;
border: 4px solid #DFE6EE ;
border-radius: 4px;
position: relative;
transform: scale(0.5);
transform-origin: left top;
&:after{
content: "";
display: block;
height: 12px;
width: 4px;
position: absolute;
background:#DFE6EE ;
right: -8px;
top: 0;
bottom: 0;
margin: auto 0;
}
.shell{
width: 100%;
height: 100%;
box-sizing: border-box;
padding: 2px;
background: #F8FAFC ;
display: grid;
grid-template-columns: repeat(6,1fr);
grid-column-gap: 2px;
.block{
width: 100%;
height: 100%;
}
}
}
</style>