通过JS代码实现获取安卓设备实时电量,也可以显示是否在充电,挺有意思的。
我测试只有安卓可以正常显示苹果好像不行。
HTML
<div class="zib-widget">
<div class="text-center">
<h3>电池</h3>
<div class="batteryShape">
<div class="level">
<div class="percentage" style="width: 100%;">
</div>
</div>
</div>
<div class="percent">100%</div>
<p class="batteryTime">正在充电</p>
</div>
</div>
CSS
.batteryShape{
position: relative;
width: 140px;
height: 65px;
margin: auto;
border: 3px solid #333;
border-radius: 10px;
}
.batteryShape::before{
content: '';
position: absolute;
top: 50%;
right: -12px;
margin-left: 2px;
transform: translateY(-50%);
width: 7px;
height: 1;
background: #333;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}
.batteryShape::after{
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 50%;
background: rgba(255,255,255,.1);
}
.level{
position: absolute;
top: 4px;
right: 4px;
bottom: 4px;
left: 4px;
border-radius: 4px;
overflow: hidden;
}
.percentage{
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 50%;
background: linear-gradient(90deg,#9c27b0,#fd2c72);
}
.batteryTime{
font-size: 0.6em;
color: #333333;
letter-spacing: -0.6px;
}
javaScript
请登录后发表评论
注册
请登录后查看评论内容