使用Vue实现简单的信号和电池电量组件

 更新时间:2025年04月04日 08:49:13   作者:脆  
这篇文章主要为大家详细介绍了如何使用Vue实现简单的信号和电池电量组件效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

vue信号组件

实现代码

  <template>
      <div class="signal-content">
          <div class="signal-bars">
              <div v-for="(n, index) in 5" :key="n" class="bar" :class="getBarClass(n)" :style="{ height: `${(index + 1) * 20}%` }"></div>
          </div>
          <span class="signal-type">{{ props.type == 1 ? '4G' : 'WIFI' }}</span>
      </div>
  </template>
  
  <script setup>
  import { computed } from 'vue';
  
  const props = defineProps({
      signalStrength: {
          type: Number,
          default: 0,
          validator: (value) => value >= 0 && value <= 5,
      },
      type: {
          type: [String, Number],
          default: '1',
      },
  });

  const getBarClass = computed(() => {
      return (index) => {
          // 如果当前索引小于或等于信号强度,则返回相应的颜色类
          if (index <= props.signalStrength) {
              if (props.signalStrength <= 1) {
                  return 'low';
              } else if (props.signalStrength <= 3) {
                  return 'medium';
              } else {
                  return 'high';
              }
          }
          return 'empty';
      };
  });
  </script>

  <style scoped>
  .signal-content {
      display: flex;
      align-items: center;
  }
  .signal-bars {
      display: flex;
      width: 20px;
      height: 14px;
      align-items: flex-end;
      margin-right: 3px;
  }

  .bar {
      width: 2px; /* 单个信号条的宽度 */
      margin-right: 2px; /* 信号条之间的间隔 */
      transition: background-color 0.3s; /* 动画效果 */
  }

  .bar:last-child {
      margin-right: 0; /* 最后一个信号条不需要右边距 */
  }

  .bar.low {
      background-color: #ff4949; /* 低信号强度颜色 */
  }

  .bar.medium {
      background-color: #ffba00; /* 中等信号强度颜色 */
  }

  .bar.high {
      background-color: #13ce66; /* 高信号强度颜色 */
  }

  .bar.empty {
      background-color: #ccc; /* 空信号条颜色 */
  }
  </style>

使用

<SignalStrength :signalStrength="item.net_strenth" :type="item.net_type" />

效果

vue电池电量组件 

实现代码

<template>
      <!-- 电池电量Icon组件 -->
      <div class="electric-panel" :class="bgClass">
          <div class="panel" :style="{ transform: `rotate(${rotate}deg)` }">
              <div class="remainder" :style="{ width: batteryNum + '%' }" />
          </div>
          <div v-show="showText" :style="{ marginLeft: (parseFloat(rotate) ? 0 : '10') + 'px' }" class="text">
              <!-- 充电中不显示电量百分比 -->
              <template v-if="!proIsCharge">{{ batteryNum }}%</template>
              <template v-else>充电中</template>
          </div>
      </div>
  </template>

  <script setup>

  const props = defineProps({
      // 电池显示的数值
      quantity: {
          type: Number,
          default: 0,
      },
      // 是否显示电量百分比
      showText: {
          type: Boolean,
          default: true,
      },
      // 是否展示充电状态
      proIsCharge: {
          type: Boolean,
          default: false,
      },
      // 旋转百分比
      rotate: {
          type: String,
          default: '0',
      },
  });

  const batteryNum = ref(props.quantity);
  const bgClass = computed(() => {
      if (props.proIsCharge) return 'primary';
      if (batteryNum.value >= 30) {
          return 'success';
      } else if (batteryNum.value >= 15) {
          return 'warning';
      } else if (batteryNum.value >= 5) {
          return 'danger';
      } else {
          return 'danger';
      }
  });

  let myInterval = null;

  const handeRecharge = () => {
      clearInterval(myInterval);
      batteryNum.value = 0;
      if (props.proIsCharge) {
          myInterval = setInterval(() => {
              drawCharge();
          }, 500);
      }
  };

  const drawCharge = () => {
      batteryNum.value += 20;
      if (batteryNum.value > 100) {
          batteryNum.value = 0;
      }
  };

  onMounted(() => {
      if (props.proIsCharge) {
          handeRecharge();
      }
  });

  onBeforeUnmount(() => {
      if (myInterval) {
          clearInterval(myInterval);
      }
  });
  </script>

  <style lang="scss" scoped>
  // custom theme color
  $color-primary: #447ced;$color-success: #13ce66;
  $color-warning: #ffba00;$color-danger: #ff4949;
  $color-info: #909399;$color-white: #fff;

  @mixin panel-style($color) {
    .panel {
      border-color: $color;
      &::before {
        background: $color;
      }
      .remainder {
        background: $color;
      }
    }
    .text {
      color: $color;
    }
  }

  .electric-panel {
    display: flex;
    justify-content: center;
    align-items: center;
    &.primary {
      @include panel-style($color-primary);
    }

    &.success {
      @include panel-style($color-success);
    }
  
    &.warning {
      @include panel-style($color-warning);
    }
  
    &.danger {
      @include panel-style($color-danger);
    }
  
    .panel {
      box-sizing: border-box;
      width: 18px;
      height: 10px;
      position: relative;
      border: 1px solid #ccc;
      padding: 1px;
      border-radius: 2px;
      &::before {
        content: "";
        border-radius: 0 1px 1px 0;
        height: 4px;
        background: #ccc;
        width: 2px;
        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: auto;
      font-size: 13px;
    }
  }

  </style>

使用

<quantity :quantity="JsonContent(item).bat_l" :proIsCharge="true" />

效果

到此这篇关于使用Vue实现简单的信号和电池电量组件的文章就介绍到这了,更多相关Vue组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue实现动态响应数据变化

    Vue实现动态响应数据变化

    本篇文章主要介绍了Vue 动态响应数据变化,通过绑定数据即可以实时改变视图显示,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • vue3 高德地图关键词搜索获取经纬度的示例代码

    vue3 高德地图关键词搜索获取经纬度的示例代码

    这篇文章主要介绍了vue3 高德地图关键词搜索获取经纬度的示例代码,需要的朋友可以参考下
    2024-08-08
  • Ant design vue中的联动选择取消操作

    Ant design vue中的联动选择取消操作

    这篇文章主要介绍了Ant design vue中的联动选择取消操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • vue实现富文本编辑器详细过程

    vue实现富文本编辑器详细过程

    Vue富文本的实现可以使用一些现成的第三方库,如Quill、Vue-quill-editor、wangEditor等,这篇文章主要给大家介绍了关于vue实现富文本编辑器的相关资料,需要的朋友可以参考下
    2024-01-01
  • defineProperty和Proxy基础功能及性能对比

    defineProperty和Proxy基础功能及性能对比

    这篇文章主要为大家介绍了defineProperty和Proxy基础功能及性能对比,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • vue中使用protobuf的过程记录

    vue中使用protobuf的过程记录

    由于目前公司采用了ProtoBuf做前后端数据交互,进公司以来一直用的是公司大神写好的基础库,完全不了解底层是如何解析的。下面小编给大家分享vue中使用protobuf的过程记录,需要的朋友参考下吧
    2018-10-10
  • elementUI自定义上传文件功能实现(前端后端超详细过程)

    elementUI自定义上传文件功能实现(前端后端超详细过程)

    自定义上传思路很简单,下面这篇文章主要给大家介绍了关于elementUI自定义上传文件功能实现(前端后端超详细过程)的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • 从零开始搭建vue移动端项目到上线的步骤

    从零开始搭建vue移动端项目到上线的步骤

    这篇文章主要介绍了从零开始搭建vue移动端项目到上线的步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • vuecli4配置sass与less详解

    vuecli4配置sass与less详解

    这篇文章主要为大家介绍了vuecli4配置sass与less详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • vue实现鼠标悬浮框效果

    vue实现鼠标悬浮框效果

    这篇文章主要为大家详细介绍了vue实现鼠标悬浮框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03

最新评论