当前位置: 首页 > news >正文

JavaScript 实现 WiFi 信号强度模拟类

wifi信号class类

class WifiSignal {
    constructor(options = {}) {
        // 合并配置
        this.config = {
            parentContainer: 'body',
            strength: 1,
            colors: {
                low: '#ff4444',
                medium: '#ffa500',
                high: '#4CAF50'
            },
            ...options
        };

        // 初始化验证
        this.validateConfig();

        // 创建DOM结构
        this.initDOM();

        // 初始渲染
        this.update(this.config.strength);
    }

    validateConfig() {
        // 容器验证
        if (!document.querySelector(this.config.parentContainer)) {
            throw new Error(`无效的父容器选择器: ${this.config.parentContainer}`);
        }

        // 强度验证
        if (this.config.strength < 1 || this.config.strength > 5) {
            throw new Error('信号强度必须介于1-5之间');
        }

        // 颜色格式验证
        const colorRegex = /^#([0-9A-F]{3}){1,2}$/i;
        Object.values(this.config.colors).forEach(color => {
            if (!colorRegex.test(color)) {
                throw new Error(`无效的颜色格式: ${color}`);
            }
        });
    }

    initDOM() {
        // 创建容器
        this.container = document.createElement('div');
        this.container.className = 'wifi-signal-wrapper';

        // 创建信号条
        this.barsContainer = document.createElement('div');
        this.barsContainer.className = 'wifi-bars-container';

        this.bars = Array.from({ length: 5 }, (_, i) => {
            const bar = document.createElement('div');
            bar.className = `wifi-bar wifi-bar-${i + 1}`;
            return bar;
        });

        // 组装结构
        this.bars.forEach(bar => this.barsContainer.appendChild(bar));
        this.container.appendChild(this.barsContainer);

        // 挂载到页面
        document.querySelector(this.config.parentContainer)
            .appendChild(this.container);
    }

    update(newStrength) {
        // 验证强度值
        const strength = Math.max(1, Math.min(5, parseInt(newStrength)));
        if (isNaN(strength)) return;

        // 确定颜色等级
        let colorLevel;
        if (strength <= 2) colorLevel = 'low';
        else if (strength === 3) colorLevel = 'medium';
        else colorLevel = 'high';

        // 更新所有信号条颜色
        this.bars.forEach((bar, index) => {
            bar.style.backgroundColor = index < strength ?
                this.config.colors[colorLevel] : '#e0e0e0';
        });
    }
}

wifi信号css

/* wifi-signal.css (组件专用样式) */
.wifi-signal-wrapper {
      display: inline-block;
      padding: 20px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.wifi-bars-container {
      display: flex;
      align-items: flex-end;
      gap: 5px;
      height: 60px;
}

.wifi-bar {
      width: 16px;
      background: #e0e0e0;
      border-radius: 3px;
      transition: background 0.3s ease;
}

.wifi-bar-1 { height: 20%; }
.wifi-bar-2 { height: 40%; }
.wifi-bar-3 { height: 60%; }
.wifi-bar-4 { height: 80%; }
.wifi-bar-5 { height: 100%; }

调用方的html

<!-- index.html (调用方示例) -->
<!DOCTYPE html>
<html>
<head>
      <meta charset="UTF-8">
      <title>WiFi信号组件调用示例</title>
      <!-- 组件样式 -->
      <link rel="stylesheet" href="index.css">
      <!-- 调用方自定义样式 -->
      <style>
        /* 调用方自己的控制按钮样式 */
        .custom-controls {
              margin-top: 20px;
              text-align: center;
            }
        .custom-controls button {
              padding: 10px 20px;
              margin: 0 5px;
              background: #f0f0f0;
              border: 1px solid #ddd;
              border-radius: 4px;
              cursor: pointer;
            }
        .custom-controls button:hover {
              background: #4CAF50;
              color: white;
            }
      </style>
</head>
<body>
  <!-- 组件挂载点 -->
  <div id="wifiMount"></div>
   
  <!-- 调用方自己的控制按钮 -->
  <div class="custom-controls">
        <button onclick="wifi.update(1)">1格</button>
        <button onclick="wifi.update(2)">2格</button>
        <button onclick="wifi.update(3)">3格</button>
        <button onclick="wifi.update(4)">4格</button>
        <button onclick="wifi.update(5)">5格</button>
      </div>

  <!-- 组件库 -->
  <script src="index.js"></script>
  <!-- 调用方初始化代码 -->
  <script>
    // 初始化组件实例
    const wifi = new WifiSignal({
        parentContainer: '#wifiMount', // 挂载点选择器
        strength: 4,          // 初始强度
        colors: {            // 可选自定义颜色
            low: '#ff4444',
            medium: '#ffa500',
            high: '#4CAF50'
        }
    });
</script>
</body>
</html>

效果展示

相关文章:

  • C++顺序栈的实现
  • 蓝桥杯比赛 python程序设计——神奇闹钟
  • 图像颜色空间对比(Opencv)
  • 【Nginx】Nginx代理Tomcat配置及404问题解决
  • JavaScript逆向工程:如何判断对称加密与非对称加密
  • LLM应用开发(七)--记忆
  • 聊一聊接口测试时遇到第三方服务时怎么办
  • map映射到二维数组
  • Windows下安装depot_tools
  • 云曦月末断网考核复现
  • 力扣HOT100之链表: 148. 排序链表
  • queue容器
  • 3.软考高项(信息系统项目管理师)-范围管理
  • Openssl升级至openssl9.8p1含全部踩坑内容
  • Socket多路复用网络编程应用总结
  • RDD行动算子和累加器
  • 低代码控件开发平台:飞帆中粘贴富文本的控件
  • C++学习中常见的数组越界问题及解决方案
  • fit 转 gpx
  • w286入校申报审批系统设计与实现
  • html5结构的网站/互联网推广员是做什么
  • 孝感网站开发选优搏/google adwords关键词工具
  • 建三江廉政建设网站/在线种子资源库
  • 页面设计读书笔记1500/搜狗seo
  • 免费做网站哪家好/佛山做网站的公司哪家好
  • 要学做网站/哪家培训机构好