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

就是这个样的粗爆,手搓一个计算器:BMI计算器

       作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。

HTML:

<div class="calculator">
<label for="height">身高 (cm):</label> 
<input id="height" step="0.1" type="number" placeholder="请输入您的身高"> 
<label for="weight">体重 (kg):</label> 
<input id="weight" step="0.1" type="number" placeholder="请输入您的体重">
<button onclick="calculateBMI()">计算</button>
<div class="result">
<p>计算结果:</p>
<p>BMI值: <span id="bmiValue">0</span></p>
<p>体重分类: <span id="bmiCategory">未知</span></p>
</div>
</div>

JS:

function calculateBMI() {const height = parseFloat(document.getElementById('height').value);const weight = parseFloat(document.getElementById('weight').value);if (isNaN(height) || isNaN(weight) || height <= 0 || weight <= 0) {alert('请输入有效的身高和体重');return;}const heightInMeters = height / 100; // 转换为米const bmi = weight / (heightInMeters * heightInMeters);document.getElementById('bmiValue').textContent = bmi.toFixed(2);let category = '';if (bmi < 18.5) {category = '偏瘦';} else if (bmi >= 18.5 && bmi < 24) {category = '正常';} else if (bmi >= 24 && bmi < 28) {category = '超重';} else {category = '肥胖';}document.getElementById('bmiCategory').textContent = category;
}

CSS:

.calculator {width: 300px;background-color: #333;color: white;padding: 20px;border-radius: 10px;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}label {display: block;margin-bottom: 10px;font-size: 16px;
}input, select {width: 100%!important;padding: 10px!important;margin-bottom: 20px;color: #000000; border-radius: 5px;border: 1px solid #555;font-size: 16px!important;background-color: #ffffff!important;
}button {width: 100%;padding: 10px;background-color: #007bff;color: white;border: none;border-radius: 5px;cursor: pointer;font-size: 16px;
}button:hover {background-color: orange;
}.result {margin-top: 20px;text-align: center;
}option {background-color: #ffffff;
}p {font-size: 18px;margin-top: 5px!important;
}

线上运行,可以直接打开:BMI计算器


http://www.mrgr.cn/news/56839.html

相关文章:

  • 写一个 qq自动回话的程序
  • 深入理解 RabbitMQ 及在.NET 中的应用
  • 速盾:cdn能加速游戏吗?
  • 计算机组成原理一句话
  • 光纤光学——弱导光纤与线偏振模
  • C语言 | Leetcode C语言题解之第493题翻转对
  • python 爬虫抓取百度热搜
  • 100种算法【Python版】第4篇——回溯法
  • 台湾精锐APEX减速机AB系列特点解析
  • vcruntime140.dll无法继续执行代码-解决方案
  • Java项目-基于springboot框架的校园志愿者管理系统项目实战(附源码+文档)
  • 羽毛球场馆预约小程序,提高场馆便捷性、利用率
  • 南京某大厂 渗透测试工程师实习面试分享
  • 证明任一双随机矩阵都可分解为若干个置换阵的乘积
  • lib静态库转为a静态库
  • QT教程-二十二,QSS界面/控件美化
  • 计算机组成原理之虚拟存储器的基本概念、计算机组成原理之页式虚拟存储器基本原理,页表,地址转换,tlb、
  • C++字符串函数(详细解析) √
  • 选对人力资源管理系统的重要性!
  • 【QT项目】QT项目综合练习之简易计数器(QT6+文件存储)
  • 大厂为什么要禁止使用数据库自增主键
  • 传统园区与智慧园区:现代化发展的差异和优势
  • @PostConstruct 注解的作用和使用
  • HTML满屏飘字代码
  • Ubuntu22.04环境搭建MQTT服务器
  • 除了HarmonyOS NEXT,华为在原生鸿蒙之夜还带来了哪些重磅新品?