207 lines
5.7 KiB
HTML
207 lines
5.7 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, "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 50px rgba(0, 0, 0, 0.2);
|
||
width: 100%;
|
||
max-width: 420px;
|
||
padding: 32px;
|
||
}
|
||
h1 {
|
||
font-size: 24px;
|
||
color: #333;
|
||
text-align: center;
|
||
margin-bottom: 6px;
|
||
}
|
||
.subtitle {
|
||
text-align: center;
|
||
color: #999;
|
||
font-size: 13px;
|
||
margin-bottom: 26px;
|
||
}
|
||
.field { margin-bottom: 18px; }
|
||
label {
|
||
display: block;
|
||
font-size: 14px;
|
||
color: #555;
|
||
margin-bottom: 8px;
|
||
font-weight: 600;
|
||
}
|
||
input {
|
||
width: 100%;
|
||
padding: 12px 14px;
|
||
border: 2px solid #e5e7eb;
|
||
border-radius: 10px;
|
||
font-size: 16px;
|
||
transition: border-color 0.2s;
|
||
outline: none;
|
||
}
|
||
input:focus { border-color: #667eea; }
|
||
button {
|
||
width: 100%;
|
||
padding: 14px;
|
||
border: none;
|
||
border-radius: 10px;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: #fff;
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
margin-top: 8px;
|
||
transition: transform 0.15s, box-shadow 0.15s;
|
||
}
|
||
button:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
|
||
}
|
||
button:active { transform: translateY(0); }
|
||
.result {
|
||
margin-top: 24px;
|
||
padding: 20px;
|
||
border-radius: 14px;
|
||
text-align: center;
|
||
display: none;
|
||
}
|
||
.result.show { display: block; animation: fade 0.3s; }
|
||
@keyframes fade {
|
||
from { opacity: 0; transform: translateY(8px); }
|
||
to { opacity: 1; transform: translateY(0); }
|
||
}
|
||
.bmi-value {
|
||
font-size: 44px;
|
||
font-weight: 800;
|
||
line-height: 1;
|
||
}
|
||
.bmi-label {
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
margin-top: 8px;
|
||
}
|
||
.scale {
|
||
margin-top: 28px;
|
||
font-size: 12px;
|
||
color: #777;
|
||
}
|
||
.scale-bar {
|
||
display: flex;
|
||
height: 10px;
|
||
border-radius: 6px;
|
||
overflow: hidden;
|
||
margin-bottom: 6px;
|
||
}
|
||
.scale-bar span { flex: 1; }
|
||
.scale-labels {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
}
|
||
.error { color: #ef4444; text-align: center; font-size: 14px; margin-top: 14px; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="card">
|
||
<h1>BMI 计算器</h1>
|
||
<p class="subtitle">身体质量指数 · 快速评估体重状况</p>
|
||
|
||
<div class="field">
|
||
<label for="height">身高(厘米 cm)</label>
|
||
<input type="number" id="height" placeholder="例如 170" step="0.1">
|
||
</div>
|
||
<div class="field">
|
||
<label for="weight">体重(千克 kg)</label>
|
||
<input type="number" id="weight" placeholder="例如 65" step="0.1">
|
||
</div>
|
||
|
||
<button onclick="calculateBMI()">计算 BMI</button>
|
||
|
||
<div id="error" class="error"></div>
|
||
|
||
<div id="result" class="result">
|
||
<div class="bmi-value" id="bmiValue">--</div>
|
||
<div class="bmi-label" id="bmiLabel">--</div>
|
||
<div class="scale">
|
||
<div class="scale-bar">
|
||
<span style="background:#60a5fa"></span>
|
||
<span style="background:#34d399"></span>
|
||
<span style="background:#fbbf24"></span>
|
||
<span style="background:#f87171"></span>
|
||
</div>
|
||
<div class="scale-labels">
|
||
<span>偏瘦</span>
|
||
<span>正常</span>
|
||
<span>超重</span>
|
||
<span>肥胖</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
// 中国成人 BMI 标准
|
||
function getCategory(bmi) {
|
||
if (bmi < 18.5) return { label: "偏瘦", color: "#3b82f6", bg: "#eff6ff" };
|
||
if (bmi < 24) return { label: "正常", color: "#10b981", bg: "#ecfdf5" };
|
||
if (bmi < 28) return { label: "超重", color: "#f59e0b", bg: "#fffbeb" };
|
||
return { label: "肥胖", color: "#ef4444", bg: "#fef2f2" };
|
||
}
|
||
|
||
function calculateBMI() {
|
||
const heightEl = document.getElementById("height");
|
||
const weightEl = document.getElementById("weight");
|
||
const errorEl = document.getElementById("error");
|
||
const resultEl = document.getElementById("result");
|
||
|
||
const h = parseFloat(heightEl.value);
|
||
const w = parseFloat(weightEl.value);
|
||
|
||
errorEl.textContent = "";
|
||
resultEl.classList.remove("show");
|
||
|
||
if (isNaN(h) || isNaN(w) || h <= 0 || w <= 0) {
|
||
errorEl.textContent = "请输入有效的身高和体重数值。";
|
||
return;
|
||
}
|
||
if (h < 50 || h > 250) {
|
||
errorEl.textContent = "身高请输入 50 ~ 250 厘米之间的数值。";
|
||
return;
|
||
}
|
||
|
||
const meters = h / 100;
|
||
const bmi = w / (meters * meters);
|
||
const cat = getCategory(bmi);
|
||
|
||
const bmiValueEl = document.getElementById("bmiValue");
|
||
const bmiLabelEl = document.getElementById("bmiLabel");
|
||
|
||
bmiValueEl.textContent = bmi.toFixed(1);
|
||
bmiValueEl.style.color = cat.color;
|
||
bmiLabelEl.textContent = "体重状况:" + cat.label;
|
||
bmiLabelEl.style.color = cat.color;
|
||
|
||
resultEl.style.background = cat.bg;
|
||
resultEl.classList.add("show");
|
||
}
|
||
|
||
// 支持回车键计算
|
||
document.addEventListener("keydown", (e) => {
|
||
if (e.key === "Enter") calculateBMI();
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|