Files
bmi-calculator/index.html
T
class07f24 55002dbef2 新增 BMI 计算器网页
我决定做一个网页版的 BMI(身体质量指数)计算器,
输入身高和体重后即可算出 BMI 数值。
2026-09-24 00:29:52 +00:00

313 lines
7.4 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BMI 计算器</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.card {
background: #fff;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 440px;
padding: 36px 32px 32px;
}
h1 {
font-size: 26px;
color: #2d3748;
text-align: center;
margin-bottom: 6px;
}
.subtitle {
text-align: center;
color: #a0aec0;
font-size: 14px;
margin-bottom: 28px;
}
.field {
margin-bottom: 20px;
}
label {
display: block;
font-size: 14px;
font-weight: 600;
color: #4a5568;
margin-bottom: 8px;
}
input[type="number"] {
width: 100%;
padding: 14px 16px;
font-size: 16px;
border: 2px solid #e2e8f0;
border-radius: 12px;
outline: none;
transition: border-color .2s, box-shadow .2s;
background: #f7fafc;
}
input[type="number"]:focus {
border-color: #667eea;
background: #fff;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}
.unit {
font-size: 12px;
color: #a0aec0;
margin-left: 6px;
font-weight: 400;
}
.btn-row {
display: flex;
gap: 12px;
margin-top: 26px;
}
button {
flex: 1;
padding: 14px;
font-size: 16px;
font-weight: 600;
border: none;
border-radius: 12px;
cursor: pointer;
transition: transform .1s, box-shadow .2s, opacity .2s;
}
button:active { transform: scale(0.98); }
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
}
.btn-primary:hover { box-shadow: 0 10px 26px rgba(102, 126, 234, 0.55); }
.btn-secondary {
background: #edf2f7;
color: #4a5568;
}
.btn-secondary:hover { background: #e2e8f0; }
.result {
margin-top: 26px;
text-align: center;
padding: 22px;
border-radius: 16px;
background: #f7fafc;
display: none;
animation: fadeIn .35s ease;
}
.result.show { display: block; }
.result .value {
font-size: 46px;
font-weight: 800;
line-height: 1.1;
color: #2d3748;
}
.result .label {
font-size: 13px;
color: #a0aec0;
margin-top: 4px;
letter-spacing: 1px;
}
.result .category {
display: inline-block;
margin-top: 14px;
padding: 8px 22px;
border-radius: 999px;
font-size: 15px;
font-weight: 700;
color: #fff;
}
.result .advice {
margin-top: 14px;
font-size: 13.5px;
color: #718096;
line-height: 1.6;
}
.error {
color: #e53e3e;
font-size: 13px;
margin-top: 12px;
text-align: center;
display: none;
}
.error.show { display: block; }
.scale {
margin-top: 22px;
font-size: 12px;
}
.scale-bar {
display: flex;
height: 8px;
border-radius: 999px;
overflow: hidden;
margin-bottom: 8px;
}
.scale-bar span { flex: 1; }
.scale-legend {
display: flex;
justify-content: space-between;
color: #a0aec0;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(8px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
</head>
<body>
<div class="card">
<h1>BMI 计算器</h1>
<p class="subtitle">身体质量指数 · Body Mass Index</p>
<div class="field">
<label>身高 <span class="unit">厘米 / cm</span></label>
<input type="number" id="height" placeholder="例如 170" min="1" step="0.1">
</div>
<div class="field">
<label>体重 <span class="unit">千克 / kg</span></label>
<input type="number" id="weight" placeholder="例如 65" min="1" step="0.1">
</div>
<div class="btn-row">
<button class="btn-primary" onclick="calculateBMI()">计算 BMI</button>
<button class="btn-secondary" onclick="resetForm()">重置</button>
</div>
<p class="error" id="error"></p>
<div class="result" id="result">
<div class="value" id="bmiValue">--</div>
<div class="label">BMI 指数 (kg/m²)</div>
<div class="category" id="category">--</div>
<div class="advice" id="advice"></div>
</div>
<div class="scale">
<div class="scale-bar">
<span style="background:#4299e1"></span>
<span style="background:#48bb78"></span>
<span style="background:#ecc94b"></span>
<span style="background:#ed8936"></span>
<span style="background:#e53e3e"></span>
</div>
<div class="scale-legend">
<span>偏瘦 &lt;18.5</span>
<span>正常 18.5-24</span>
<span>超重 24-28</span>
<span>肥胖 &gt;28</span>
</div>
</div>
</div>
<script>
function calculateBMI() {
const heightEl = document.getElementById('height');
const weightEl = document.getElementById('weight');
const errorEl = document.getElementById('error');
const resultEl = document.getElementById('result');
const heightCm = parseFloat(heightEl.value);
const weightKg = parseFloat(weightEl.value);
errorEl.classList.remove('show');
resultEl.classList.remove('show');
// 输入验证
if (isNaN(heightCm) || isNaN(weightKg)) {
showError('请输入有效的身高和体重数值。');
return;
}
if (heightCm <= 0 || weightKg <= 0) {
showError('身高和体重必须大于 0。');
return;
}
if (heightCm > 300 || weightKg > 500) {
showError('请输入合理的数值(身高 ≤ 300cm,体重 ≤ 500kg)。');
return;
}
const heightM = heightCm / 100;
const bmi = weightKg / (heightM * heightM);
// 中国人群体质标准
let category, color, advice;
if (bmi < 18.5) {
category = '偏瘦'; color = '#4299e1';
advice = '建议适当增加营养摄入,保持均衡饮食与规律作息。';
} else if (bmi < 24) {
category = '正常'; color = '#48bb78';
advice = '身材很标准,请继续保持健康的饮食和运动习惯!';
} else if (bmi < 28) {
category = '超重'; color = '#ed8936';
advice = '建议控制饮食、增加有氧运动,逐步降低体重。';
} else {
category = '肥胖'; color = '#e53e3e';
advice = '建议咨询专业医生或营养师,制定科学的减重计划。';
}
document.getElementById('bmiValue').textContent = bmi.toFixed(1);
const catEl = document.getElementById('category');
catEl.textContent = category;
catEl.style.background = color;
document.getElementById('advice').textContent = advice;
resultEl.classList.add('show');
}
function showError(msg) {
const errorEl = document.getElementById('error');
errorEl.textContent = msg;
errorEl.classList.add('show');
}
function resetForm() {
document.getElementById('height').value = '';
document.getElementById('weight').value = '';
document.getElementById('error').classList.remove('show');
document.getElementById('result').classList.remove('show');
}
// 回车键触发计算
document.addEventListener('keydown', function (e) {
if (e.key === 'Enter') calculateBMI();
});
</script>
</body>
</html>