作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class="calculator"><label for="gender">性别:</label><select id="gender"><option value="male">男性</option><option value="female">女性</option></select><label for="age">年龄 (岁):</label> <input id="age" required="" type="number" placeholder="请输入年龄"> <label for="weight">体重 (kg):</label> <input id="weight" required="" type="number" placeholder="请输入体重"> <label for="height">身高 (cm):</label> <input id="height" required="" type="number" placeholder="请输入身高"><button onclick="calculateBEE()">计算BEE</button><div id="result" class="result"></div></div>
JS:
function calculateBEE() {
var gender = document.getElementById("gender").value;
var age = parseFloat(document.getElementById("age").value);
var weight = parseFloat(document.getElementById("weight").value);
var height = parseFloat(document.getElementById("height").value);
if (isNaN(age) || isNaN(weight) || isNaN(height) || age <= 0 || weight <= 0 || height <= 0) {
document.getElementById("result").innerHTML = "请输入有效的数值!";
return;
}
var bee = 0;
// 使用 Harris-Benedict 方程计算基础代谢率
if (gender === "male") {
bee = 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age);
} else if (gender === "female") {
bee = 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age);
}
document.getElementById("result").innerHTML = "您的基础代谢率 (BEE) 为: " + bee.toFixed(2) + " 千卡/天";
}
CSS:
.calculator {
width: 100%;
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 {
font-size: 18px;
margin-top: 20px;
text-align: center;
}
option {
background-color: #ffffff;
}
p {
font-size: 18px;
margin-top: 5px!important;
}