Files

205 lines
9.8 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>每日热量需求计算器(青少年 + 成人)</title>
<style>
:root { --fg:#1f2937; --muted:#6b7280; --line:#e5e7eb; --accent:#2563eb; --bg:#f8fafc; }
* { box-sizing: border-box; }
body { margin:0; padding:24px 16px 48px; font-family:-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;
color:var(--fg); background:var(--bg); line-height:1.6; }
.wrap { max-width:760px; margin:0 auto; }
h1 { font-size:22px; margin:0 0 4px; }
.sub { color:var(--muted); font-size:14px; margin:0 0 20px; }
.card { background:#fff; border:1px solid var(--line); border-radius:12px; padding:18px; margin-bottom:18px; }
.grid { display:grid; grid-template-columns:repeat(auto-fit,minmax(150px,1fr)); gap:14px; }
label { display:flex; flex-direction:column; gap:6px; font-size:14px; color:var(--muted); }
input,select { padding:9px 10px; font-size:15px; color:var(--fg); border:1px solid var(--line);
border-radius:8px; background:#fff; }
button { margin-top:16px; width:100%; padding:12px; font-size:16px; font-weight:600; color:#fff;
background:var(--accent); border:0; border-radius:8px; cursor:pointer; }
button:hover { background:#1d4ed8; }
table { width:100%; border-collapse:collapse; font-size:15px; }
th,td { text-align:left; padding:9px 6px; border-bottom:1px solid var(--line); vertical-align:top; }
th { color:var(--muted); font-weight:500; width:44%; }
.big { font-size:22px; font-weight:700; color:var(--accent); }
.tag { display:inline-block; padding:2px 10px; border-radius:99px; font-size:13px; background:#ecfdf5; color:#047857; }
.warn { background:#fffbeb; color:#92400e; }
.foot { font-size:13px; color:var(--muted); }
</style>
</head>
<body>
<div class="wrap">
<h1>每日热量需求计算器</h1>
<p class="sub">输入性别 / 年龄 / 身高 / 体重 / 每日步数 → BMI(含年龄别判定)、BMR、PAL、TDEE、目标摄入</p>
<div class="card">
<div class="grid">
<label>性别
<select id="sex"><option value="male">男</option><option value="female">女</option></select>
</label>
<label>年龄(岁)<input id="age" type="number" value="15" step="0.5" min="3" max="100"></label>
<label>身高(cm)<input id="height" type="number" value="173" step="0.5"></label>
<label>体重(kg)<input id="weight" type="number" value="55" step="0.1"></label>
<label>每日步数<input id="steps" type="number" value="5000" step="500" min="0"></label>
</div>
<button id="go">计算</button>
</div>
<div id="out"></div>
<p class="foot">
结果为统计学估算(误差约 ±10%,约 ±200 kcal),不构成医学建议。未成年人请在家长或医生指导下调整饮食。
年龄别 BMI 界值为按 WS/T 456-2014、WS/T 586-2018 整理的近似值,正式判定以标准原文或医院生长发育评估为准。
</p>
</div>
<script>
// ---- 年龄别 BMI 界值(与 cutoffs.json 一致) ----
const CUTOFFS = {
male: {
"7.0":{thin:13.9,overweight:17.4,obese:19.2}, "8.0":{thin:14.2,overweight:18.1,obese:20.3},
"9.0":{thin:14.5,overweight:18.9,obese:21.4}, "10.0":{thin:14.9,overweight:19.6,obese:22.5},
"11.0":{thin:15.3,overweight:20.3,obese:23.6}, "12.0":{thin:15.7,overweight:21.0,obese:24.7},
"13.0":{thin:16.0,overweight:21.9,obese:25.7}, "14.0":{thin:16.3,overweight:22.6,obese:26.4},
"15.0":{thin:16.6,overweight:23.1,obese:26.9}, "16.0":{thin:16.9,overweight:23.3,obese:27.1},
"17.0":{thin:17.2,overweight:23.7,obese:27.6}, "18.0":{thin:17.4,overweight:24.0,obese:28.0}
},
female: {
"7.0":{thin:13.6,overweight:16.8,obese:18.5}, "8.0":{thin:13.9,overweight:17.4,obese:19.4},
"9.0":{thin:14.3,overweight:18.1,obese:20.4}, "10.0":{thin:14.6,overweight:18.9,obese:21.5},
"11.0":{thin:15.1,overweight:19.9,obese:22.7}, "12.0":{thin:15.6,overweight:20.9,obese:23.9},
"13.0":{thin:16.1,overweight:21.9,obese:25.2}, "14.0":{thin:16.6,overweight:22.6,obese:26.2},
"15.0":{thin:17.0,overweight:23.0,obese:26.8}, "16.0":{thin:17.3,overweight:23.2,obese:27.2},
"17.0":{thin:17.5,overweight:23.4,obese:27.6}, "18.0":{thin:17.6,overweight:24.0,obese:28.0}
}
};
const PAL_ANCHORS = [[0,1.20],[2500,1.30],[5000,1.45],[7500,1.525],[10000,1.60],[12500,1.70],[15000,1.80],[20000,1.95]];
const PAL_JITTER = 0.05;
const MJCAL = 239.006;
function palFromSteps(steps){
if (steps <= PAL_ANCHORS[0][0]) return PAL_ANCHORS[0][1];
for (let i=0;i<PAL_ANCHORS.length-1;i++){
const [s0,p0] = PAL_ANCHORS[i], [s1,p1] = PAL_ANCHORS[i+1];
if (steps <= s1) return +(p0 + (steps-s0)/(s1-s0)*(p1-p0)).toFixed(3);
}
return PAL_ANCHORS[PAL_ANCHORS.length-1][1];
}
function interpCutoff(table, age){
const ages = Object.keys(table).map(Number).sort((a,b)=>a-b);
if (age <= ages[0]) return Object.assign({}, table[ages[0].toFixed(1)]);
const last = ages[ages.length-1];
if (age >= last) return Object.assign({}, table[last.toFixed(1)]);
for (let i=0;i<ages.length-1;i++){
const a0 = ages[i], a1 = ages[i+1];
if (age <= a1){
const r = (age-a0)/(a1-a0), lo = table[a0.toFixed(1)], hi = table[a1.toFixed(1)];
const out = {};
for (const k in lo) out[k] = +(lo[k] + r*(hi[k]-lo[k])).toFixed(2);
return out;
}
}
return Object.assign({}, table[last.toFixed(1)]);
}
function classifyBmi(bmi, age, sex){
if (age >= 7 && age < 18){
const c = interpCutoff(CUTOFFS[sex], age);
const basis = `年龄别界值:消瘦 <${c.thin};超重 ≥${c.overweight};肥胖 ≥${c.obese}`;
if (bmi < c.thin) return ["消瘦", basis];
if (bmi >= c.obese) return ["肥胖", basis];
if (bmi >= c.overweight) return ["超重", basis];
return ["正常", basis];
}
const basis = "成人标准:偏瘦 <18.5;正常 18.5-23.9;超重 24-27.9;肥胖 ≥28";
if (bmi < 18.5) return ["偏瘦", basis];
if (bmi < 24) return ["正常", basis];
if (bmi < 28) return ["超重", basis];
return ["肥胖", basis];
}
function bmrSchofield(sex, age, w){
let mj;
if (age < 3) mj = sex==="male" ? 0.249*w-0.127 : 0.244*w-0.130;
else if (age < 10) mj = sex==="male" ? 0.095*w+2.110 : 0.085*w+2.033;
else if (age < 18) mj = sex==="male" ? 0.074*w+2.754 : 0.056*w+2.898;
else if (age < 30) mj = sex==="male" ? 0.063*w+2.896 : 0.062*w+2.036;
else if (age < 60) mj = sex==="male" ? 0.048*w+3.653 : 0.034*w+3.538;
else mj = sex==="male" ? 0.049*w+2.459 : 0.038*w+2.755;
return mj * MJCAL;
}
function bmrMifflin(sex, age, h, w){ return 10*w + 6.25*h - 5*age + (sex==="male" ? 5 : -161); }
function calciumTarget(age){
if (age <= 6) return 200;
if (age <= 10) return 1000;
if (age <= 13) return 1200;
if (age <= 17) return 1000;
if (age <= 49) return 800;
return 1000;
}
function buildReport(sex, age, h, w, steps){
const bmi = w / Math.pow(h/100, 2);
const [category, basis] = classifyBmi(bmi, age, sex);
const useKid = age < 18;
const bmr = useKid ? bmrSchofield(sex, age, w) : bmrMifflin(sex, age, h, w);
const mifflin = bmrMifflin(sex, age, h, w);
const pal = palFromSteps(steps);
const growth = age < 10 ? 20 : (age < 18 ? 25 : 0);
const tdee = bmr*pal + growth;
const low = bmr*Math.max(pal-PAL_JITTER,1.0) + growth;
const high = bmr*(pal+PAL_JITTER) + growth;
const upper = (category === "正常" && bmi < 20) ? 1.1 : 1.05;
return {
bmi, category, basis,
bmr: Math.round(bmr), formula: useKid ? "Schofield(青少年)" : "Mifflin-St Jeor(成人)",
mifflin: Math.round(mifflin), pal, growth,
tdee: Math.round(tdee), low: Math.round(low), high: Math.round(high),
target: [Math.round(tdee), Math.round(tdee*upper)],
protein: [Math.round(w*1.2), Math.round(w*1.5)],
calcium: calciumTarget(age)
};
}
function render(r, sex, age, h, w, steps){
const cls = (r.category === "正常") ? "tag" : "tag warn";
document.getElementById("out").innerHTML = `
<div class="card">
<table>
<tr><th>输入</th><td>${sex==="male"?"男":"女"} / ${age} 岁 / ${h} cm / ${w} kg / 每天约 ${steps} 步</td></tr>
<tr><th>① BMI</th><td><span class="big">${r.bmi.toFixed(2)}</span>
<span class="${cls}">${r.category}</span><br><span class="foot">${r.basis}</span></td></tr>
<tr><th>② BMR 基础代谢</th><td><b>${r.bmr}</b> kcal/天 <span class="foot">[${r.formula}]</span><br>
<span class="foot">对照:成人 Mifflin 公式 = ${r.mifflin} kcal/天</span></td></tr>
<tr><th>③ PAL 活动系数</th><td><b>${r.pal}</b> <span class="foot">(每天约 ${steps} 步)</span></td></tr>
<tr><th>④ TDEE 维持热量</th><td><span class="big">${r.tdee}</span> kcal/天
<span class="foot">(区间 ${r.low} – ${r.high})</span>
${r.growth ? `<br><span class="foot">已含生长发育额外耗能 +${r.growth} kcal/天</span>` : ""}</td></tr>
<tr><th>⑤ 建议每日摄入</th><td><span class="big">${r.target[0]} – ${r.target[1]}</span> kcal/天<br>
<span class="foot">蛋白质 ${r.protein[0]} – ${r.protein[1]} g/天,钙 ≥ ${r.calcium} mg/天</span></td></tr>
</table>
</div>`;
}
document.getElementById("go").addEventListener("click", () => {
const sex = document.getElementById("sex").value;
const age = parseFloat(document.getElementById("age").value);
const h = parseFloat(document.getElementById("height").value);
const w = parseFloat(document.getElementById("weight").value);
const steps = parseInt(document.getElementById("steps").value, 10);
if (!(age > 0) || !(h > 0) || !(w > 0) || !(steps >= 0)) {
document.getElementById("out").innerHTML = '<div class="card">请输入有效的年龄 / 身高 / 体重 / 步数。</div>';
return;
}
render(buildReport(sex, age, h, w, steps), sex, age, h, w, steps);
});
document.getElementById("go").click();
</script>
</body>
</html>