311 lines
10 KiB
HTML
311 lines
10 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; }
|
||
body {
|
||
margin: 0;
|
||
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
|
||
background: linear-gradient(135deg, #eef2ff 0%, #f0fdf4 100%);
|
||
color: #1f2937;
|
||
padding: 26px 16px 50px;
|
||
}
|
||
.wrap { max-width: 860px; margin: 0 auto; }
|
||
h1 { font-size: 25px; margin: 0 0 6px; text-align: center; }
|
||
.sub { text-align: center; color: #6b7280; font-size: 13px; margin-bottom: 22px; }
|
||
.card {
|
||
background: #fff;
|
||
border-radius: 16px;
|
||
padding: 22px;
|
||
box-shadow: 0 4px 20px rgba(0, 0, 0, .06);
|
||
margin-bottom: 18px;
|
||
}
|
||
.inputs { display: flex; flex-wrap: wrap; gap: 16px; }
|
||
.field { flex: 1 1 160px; }
|
||
label { display: block; font-size: 12px; color: #6b7280; margin-bottom: 6px; }
|
||
input, select {
|
||
width: 100%;
|
||
padding: 11px 12px;
|
||
border: 1px solid #d1d5db;
|
||
border-radius: 10px;
|
||
font-size: 16px;
|
||
outline: none;
|
||
background: #fff;
|
||
transition: border-color .15s;
|
||
}
|
||
input:focus, select:focus { border-color: #6366f1; }
|
||
|
||
.result { text-align: center; padding: 6px 0 0; }
|
||
.bmi-num { font-size: 54px; font-weight: 700; line-height: 1; letter-spacing: -1px; }
|
||
.badge {
|
||
display: inline-block;
|
||
margin-top: 12px;
|
||
padding: 5px 18px;
|
||
border-radius: 999px;
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
color: #fff;
|
||
}
|
||
.bar {
|
||
position: relative;
|
||
height: 12px;
|
||
border-radius: 999px;
|
||
margin: 24px 0 0;
|
||
}
|
||
.marker {
|
||
position: absolute;
|
||
top: -5px;
|
||
width: 4px;
|
||
height: 22px;
|
||
background: #111827;
|
||
border-radius: 2px;
|
||
transform: translateX(-50%);
|
||
transition: left .25s ease;
|
||
box-shadow: 0 0 0 2px #fff;
|
||
}
|
||
.ticks { position: relative; height: 18px; margin-top: 6px; }
|
||
.ticks span {
|
||
position: absolute;
|
||
font-size: 11px;
|
||
color: #9ca3af;
|
||
transform: translateX(-50%);
|
||
white-space: nowrap;
|
||
}
|
||
.range-note { text-align: center; font-size: 13px; color: #4b5563; margin-top: 18px; }
|
||
.range-note b { color: #10b981; }
|
||
|
||
h2 { font-size: 16px; margin: 0 0 14px; display: flex; align-items: center; gap: 8px; }
|
||
h2::before { content: ''; width: 4px; height: 16px; border-radius: 2px; background: #6366f1; }
|
||
table { width: 100%; border-collapse: collapse; font-size: 14px; }
|
||
th, td { padding: 11px 10px; text-align: center; border-bottom: 1px solid #f1f1f1; }
|
||
th { background: #f9fafb; color: #4b5563; font-weight: 600; font-size: 13px; }
|
||
tr:last-child td { border-bottom: none; }
|
||
tbody tr.me { background: #eef2ff; font-weight: 700; }
|
||
tbody tr.me td:first-child::after { content: ' ← 你'; color: #6366f1; font-size: 12px; }
|
||
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
|
||
.hint { font-size: 12px; color: #9ca3af; margin-top: 12px; line-height: 1.7; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="wrap">
|
||
<h1>BMI 值计算表格</h1>
|
||
<p class="sub">输入身高与体重,自动计算 BMI 并生成对照表</p>
|
||
|
||
<div class="card">
|
||
<div class="inputs">
|
||
<div class="field">
|
||
<label>身高(厘米)</label>
|
||
<input id="height" type="number" value="171" min="50" max="250" step="0.1">
|
||
</div>
|
||
<div class="field">
|
||
<label>体重(公斤)</label>
|
||
<input id="weight" type="number" value="63" min="10" max="300" step="0.1">
|
||
</div>
|
||
<div class="field">
|
||
<label>分类标准</label>
|
||
<select id="standard">
|
||
<option value="cn">中国成人标准</option>
|
||
<option value="who">WHO 国际标准</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="result">
|
||
<div class="bmi-num" id="bmiNum">--</div>
|
||
<div><span class="badge" id="badge" style="background:#9ca3af">等待输入</span></div>
|
||
</div>
|
||
|
||
<div class="bar" id="bar"><div class="marker" id="marker" style="left:0; display:none"></div></div>
|
||
<div class="ticks" id="ticks"></div>
|
||
|
||
<div class="range-note" id="rangeNote">—</div>
|
||
</div>
|
||
|
||
<div class="card">
|
||
<h2>分类标准表</h2>
|
||
<table>
|
||
<thead>
|
||
<tr><th>分类</th><th>BMI 范围</th><th>说明</th></tr>
|
||
</thead>
|
||
<tbody id="catBody"></tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<div class="card">
|
||
<h2>按你的身高算:体重 ↔ BMI 对照表</h2>
|
||
<table>
|
||
<thead>
|
||
<tr><th>体重 (kg)</th><th>BMI</th><th>评价</th></tr>
|
||
</thead>
|
||
<tbody id="gridBody"></tbody>
|
||
</table>
|
||
<p class="hint">加粗行即为你当前体重所在的位置。公式:BMI = 体重(kg) ÷ 身高(m)²</p>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
const THRESHOLDS = {
|
||
cn: [
|
||
{ max: 18.5, name: '偏瘦', color: '#3b82f6' },
|
||
{ max: 24.0, name: '正常', color: '#10b981' },
|
||
{ max: 28.0, name: '超重', color: '#f59e0b' },
|
||
{ max: Infinity, name: '肥胖', color: '#ef4444' }
|
||
],
|
||
who: [
|
||
{ max: 18.5, name: '偏瘦', color: '#3b82f6' },
|
||
{ max: 25.0, name: '正常', color: '#10b981' },
|
||
{ max: 30.0, name: '超重', color: '#f59e0b' },
|
||
{ max: Infinity, name: '肥胖', color: '#ef4444' }
|
||
]
|
||
};
|
||
const STANDARD_LABEL = { cn: '中国成人标准', who: 'WHO 国际标准' };
|
||
const MIN = 15, MAX = 35; // 刻度条显示的 BMI 范围
|
||
|
||
function classify(bmi, std) {
|
||
const list = THRESHOLDS[std];
|
||
for (let i = 0; i < list.length; i++) {
|
||
if (bmi < list[i].max) return list[i];
|
||
}
|
||
return list[list.length - 1];
|
||
}
|
||
|
||
function buildGradient(std) {
|
||
const t = THRESHOLDS[std];
|
||
const colors = ['#3b82f6', '#10b981', '#f59e0b', '#ef4444'];
|
||
let stops = [], prev = 0;
|
||
for (let i = 0; i < 3; i++) {
|
||
const p = ((t[i].max - MIN) / (MAX - MIN)) * 100;
|
||
stops.push(colors[i] + ' ' + prev + '% ' + p + '%');
|
||
prev = p;
|
||
}
|
||
stops.push(colors[3] + ' ' + prev + '% 100%');
|
||
return 'linear-gradient(90deg, ' + stops.join(', ') + ')';
|
||
}
|
||
|
||
function renderTicks(std) {
|
||
const t = THRESHOLDS[std];
|
||
const box = document.getElementById('ticks');
|
||
box.innerHTML = '';
|
||
[MIN, t[0].max, t[1].max, t[2].max, MAX].forEach(function (v) {
|
||
const span = document.createElement('span');
|
||
span.textContent = v;
|
||
span.style.left = ((v - MIN) / (MAX - MIN) * 100) + '%';
|
||
box.appendChild(span);
|
||
});
|
||
}
|
||
|
||
function renderCategoryTable(std) {
|
||
const t = THRESHOLDS[std];
|
||
const body = document.getElementById('catBody');
|
||
body.innerHTML = '';
|
||
let low = 0;
|
||
t.forEach(function (item) {
|
||
const tr = document.createElement('tr');
|
||
const range = item.max === Infinity
|
||
? '≥ ' + low
|
||
: low + ' ~ ' + item.max;
|
||
const advice = {
|
||
'偏瘦': '建议适当增重',
|
||
'正常': '保持即可',
|
||
'超重': '建议控制饮食',
|
||
'肥胖': '建议就医咨询'
|
||
}[item.name] || '';
|
||
tr.innerHTML =
|
||
'<td><span class="dot" style="background:' + item.color + '"></span>' + item.name + '</td>' +
|
||
'<td>' + range + '</td>' +
|
||
'<td style="color:#9ca3af">' + advice + '</td>';
|
||
body.appendChild(tr);
|
||
low = item.max;
|
||
});
|
||
}
|
||
|
||
function renderGrid(heightCm, weightKg, std) {
|
||
const h = heightCm / 100;
|
||
const body = document.getElementById('gridBody');
|
||
body.innerHTML = '';
|
||
if (!h || h <= 0) return;
|
||
|
||
// 生成 45~90kg,步长 5
|
||
const weights = [];
|
||
for (let w = 45; w <= 90; w += 5) weights.push(w);
|
||
// 把当前体重也插进去,方便对照
|
||
if (weightKg && weights.indexOf(weightKg) === -1) weights.push(weightKg);
|
||
weights.sort(function (a, b) { return a - b; });
|
||
|
||
let closest = null, minDiff = Infinity;
|
||
weights.forEach(function (w) {
|
||
const d = Math.abs(w - weightKg);
|
||
if (d < minDiff) { minDiff = d; closest = w; }
|
||
});
|
||
|
||
weights.forEach(function (w) {
|
||
const bmi = w / (h * h);
|
||
const cat = classify(bmi, std);
|
||
const tr = document.createElement('tr');
|
||
if (w === closest && weightKg) tr.className = 'me';
|
||
tr.innerHTML =
|
||
'<td>' + w + '</td>' +
|
||
'<td>' + bmi.toFixed(2) + '</td>' +
|
||
'<td><span class="dot" style="background:' + cat.color + '"></span>' + cat.name + '</td>';
|
||
body.appendChild(tr);
|
||
});
|
||
}
|
||
|
||
function update() {
|
||
const heightCm = parseFloat(document.getElementById('height').value);
|
||
const weightKg = parseFloat(document.getElementById('weight').value);
|
||
const std = document.getElementById('standard').value;
|
||
|
||
document.getElementById('bar').style.background = buildGradient(std);
|
||
renderTicks(std);
|
||
renderCategoryTable(std);
|
||
|
||
const ok = heightCm > 0 && weightKg > 0;
|
||
if (!ok) {
|
||
document.getElementById('bmiNum').textContent = '--';
|
||
const badge = document.getElementById('badge');
|
||
badge.textContent = '等待输入';
|
||
badge.style.background = '#9ca3af';
|
||
document.getElementById('marker').style.display = 'none';
|
||
document.getElementById('rangeNote').textContent = '—';
|
||
renderGrid(heightCm, 0, std);
|
||
return;
|
||
}
|
||
|
||
const h = heightCm / 100;
|
||
const bmi = weightKg / (h * h);
|
||
const cat = classify(bmi, std);
|
||
const t = THRESHOLDS[std];
|
||
|
||
document.getElementById('bmiNum').textContent = bmi.toFixed(2);
|
||
document.getElementById('bmiNum').style.color = cat.color;
|
||
const badge = document.getElementById('badge');
|
||
badge.textContent = cat.name;
|
||
badge.style.background = cat.color;
|
||
|
||
const marker = document.getElementById('marker');
|
||
marker.style.display = 'block';
|
||
const pos = Math.max(0, Math.min(100, (bmi - MIN) / (MAX - MIN) * 100));
|
||
marker.style.left = pos + '%';
|
||
|
||
const low = 18.5 * h * h;
|
||
const high = t[1].max * h * h;
|
||
document.getElementById('rangeNote').innerHTML =
|
||
'你的身高下,<b>' + t[1].name + '</b>体重范围为 ' +
|
||
low.toFixed(1) + ' ~ ' + high.toFixed(1) + ' kg(当前 ' + weightKg + ' kg)';
|
||
|
||
renderGrid(heightCm, weightKg, std);
|
||
}
|
||
|
||
['height', 'weight', 'standard'].forEach(function (id) {
|
||
document.getElementById(id).addEventListener('input', update);
|
||
document.getElementById(id).addEventListener('change', update);
|
||
});
|
||
update();
|
||
</script>
|
||
</body>
|
||
</html>
|