vue+less 电池电量图标组件

父组件调用

 <battery-icon :quantity=40  style="width:70px;"></battery-icon>

组件代码

<template>
  <div class="electric-panel" :class="bgClass">
    <div class="panel" :style="{transform: `rotate(${rotate}deg)`}">
      <div class="remainder" :style="{width: quantity +'%'}" />
    </div>
    <div v-show="showText" :style="{marginLeft: (parseFloat(rotate)? 0 : '10') + 'px'}"
      class="text">{{ quantity }}%</div>
  </div>

</template>

<script>
/**
 * 电池电量Icon
 */
export default {
  props: {
  	// 电量值
    quantity: {
      type: Number,
      default: 0
    },
    // 是否显示电量值
    showText: {
      type: Boolean,
      default: true
    },
    // 旋转角度
    rotate: {
      type: String,
      default: '0'
    }
  },
  computed: {
    bgClass() {
      if (this.quantity >= 50) {
        return 'success'
      }
      //  else if (this.quantity >= 15) {
      //   return 'warning'
      // }
      //  else if (this.quantity >= 1) {
      //   return 'danger'
      // } 
      else {
        return 'danger'
      }
    }
  }
}
</script>

<style lang="less" scoped>
@color-success: #34A94D;
// @color-warning: orange;
@color-danger: #FA3220 ;
.panel(@color) {
  .panel {
    border-color: @color;
    &:before {
      background: @color;
    }
    .remainder {
      background: @color;
    }
  }
  .text {
    color: @color;
  }
}
.electric-panel {
  display: flex;
  justify-content: center;
  align-items: center;

  .panel {
    box-sizing: border-box;
    width: 30px;
    height: 14px;
    position: relative;
    border: 2px solid #ccc;
    padding: 1px;
    border-radius: 3px;

    &::before {
      content: '';
      border-radius: 0 1px 1px 0;
      height: 6px;
      background: #ccc;
      width: 4px;
      position: absolute;
      top: 50%;
      right: -4px;
      transform: translateY(-50%);
    }

    .remainder {
      border-radius: 1px;
      position: relative;
      height: 100%;
      width: 0%;
      left: 0;
      top: 0;
      background: #fff;
    }
  }

  .text {
    text-align: left;
    width: 42px;
  }

  &.success {
    .panel(@color-success);
  }

  // &.warning {
  //   .panel(@color-warning);
  // }

  &.danger {
    .panel(@color-danger);
  }
}
</style>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值