使用Vue實現(xiàn)簡單的信號和電池電量組件
更新時間:2025年04月04日 08:49:13 作者:脆
這篇文章主要為大家詳細介紹了如何使用Vue實現(xiàn)簡單的信號和電池電量組件效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
vue信號組件
實現(xiàn)代碼
<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) => { // 如果當前索引小于或等于信號強度,則返回相應(yīng)的顏色類 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電池電量組件
實現(xiàn)代碼
<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({ // 電池顯示的數(shù)值 quantity: { type: Number, default: 0, }, // 是否顯示電量百分比 showText: { type: Boolean, default: true, }, // 是否展示充電狀態(tài) proIsCharge: { type: Boolean, default: false, }, // 旋轉(zhuǎn)百分比 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" />
效果
到此這篇關(guān)于使用Vue實現(xiàn)簡單的信號和電池電量組件的文章就介紹到這了,更多相關(guān)Vue組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實現(xiàn)動態(tài)響應(yīng)數(shù)據(jù)變化
本篇文章主要介紹了Vue 動態(tài)響應(yīng)數(shù)據(jù)變化,通過綁定數(shù)據(jù)即可以實時改變視圖顯示,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04vue3 高德地圖關(guān)鍵詞搜索獲取經(jīng)緯度的示例代碼
這篇文章主要介紹了vue3 高德地圖關(guān)鍵詞搜索獲取經(jīng)緯度的示例代碼,需要的朋友可以參考下2024-08-08Ant design vue中的聯(lián)動選擇取消操作
這篇文章主要介紹了Ant design vue中的聯(lián)動選擇取消操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10defineProperty和Proxy基礎(chǔ)功能及性能對比
這篇文章主要為大家介紹了defineProperty和Proxy基礎(chǔ)功能及性能對比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08elementUI自定義上傳文件功能實現(xiàn)(前端后端超詳細過程)
自定義上傳思路很簡單,下面這篇文章主要給大家介紹了關(guān)于elementUI自定義上傳文件功能實現(xiàn)(前端后端超詳細過程)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-11-11