生成计算 BMI 与每日热量的计算器
This commit is contained in:
+276
@@ -0,0 +1,276 @@
|
|||||||
|
<!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}
|
||||||
|
:root{
|
||||||
|
--bg:#f4f6fa; --card:#fff; --ink:#1f2937; --muted:#6b7280;
|
||||||
|
--line:#e5e7eb; --brand:#2563eb;
|
||||||
|
--thin:#3b82f6; --normal:#22c55e; --over:#f59e0b; --obese:#ef4444;
|
||||||
|
}
|
||||||
|
body{
|
||||||
|
margin:0; padding:28px 16px 56px; background:var(--bg); color:var(--ink);
|
||||||
|
font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Hiragino Sans GB","Microsoft YaHei",sans-serif;
|
||||||
|
-webkit-font-smoothing:antialiased;
|
||||||
|
}
|
||||||
|
.app{max-width:780px;margin:0 auto}
|
||||||
|
header{text-align:center;margin-bottom:24px}
|
||||||
|
h1{font-size:25px;margin:0 0 8px;letter-spacing:-.3px}
|
||||||
|
.sub{margin:0;color:var(--muted);font-size:14px;line-height:1.6}
|
||||||
|
.card{
|
||||||
|
background:var(--card);border-radius:16px;padding:22px;margin-bottom:16px;
|
||||||
|
box-shadow:0 1px 2px rgba(16,24,40,.05), 0 10px 28px rgba(16,24,40,.06);
|
||||||
|
}
|
||||||
|
.card h2{font-size:15px;margin:0 0 16px;color:var(--muted);font-weight:700;letter-spacing:.3px}
|
||||||
|
.grid{display:grid;grid-template-columns:1fr 1fr;gap:16px}
|
||||||
|
.grid .full{grid-column:1 / -1}
|
||||||
|
@media(max-width:560px){.grid{grid-template-columns:1fr}}
|
||||||
|
.field{display:flex;flex-direction:column;gap:7px}
|
||||||
|
.lbl{font-size:13px;font-weight:600;color:var(--muted)}
|
||||||
|
input[type=number]{
|
||||||
|
width:100%;padding:11px 13px;border:1px solid var(--line);border-radius:10px;
|
||||||
|
font-size:15px;font-family:inherit;color:var(--ink);background:#fff;transition:.15s;
|
||||||
|
}
|
||||||
|
input[type=number]:focus{outline:none;border-color:var(--brand);box-shadow:0 0 0 3px rgba(37,99,235,.13)}
|
||||||
|
.hint{font-size:12px;color:#9ca3af}
|
||||||
|
.seg{display:flex;background:#f1f5f9;border-radius:10px;padding:4px;gap:4px}
|
||||||
|
.seg label{flex:1;cursor:pointer}
|
||||||
|
.seg input{display:none}
|
||||||
|
.seg span{
|
||||||
|
display:block;text-align:center;padding:8px 0;border-radius:8px;
|
||||||
|
font-size:14px;font-weight:600;color:var(--muted);transition:.15s;
|
||||||
|
}
|
||||||
|
.seg input:checked + span{background:#fff;color:var(--brand);box-shadow:0 1px 2px rgba(16,24,40,.12)}
|
||||||
|
|
||||||
|
/* ---- BMI 结果 ---- */
|
||||||
|
.bmi-top{display:flex;align-items:flex-end;gap:14px;flex-wrap:wrap}
|
||||||
|
.bmi-num{font-size:48px;font-weight:800;letter-spacing:-1.5px;line-height:1}
|
||||||
|
.badge{padding:5px 14px;border-radius:999px;font-size:13px;font-weight:700;color:#fff;margin-bottom:6px}
|
||||||
|
.badge.thin{background:var(--thin)} .badge.normal{background:var(--normal)}
|
||||||
|
.badge.over{background:var(--over)} .badge.obese{background:var(--obese)}
|
||||||
|
.scale{margin-top:22px}
|
||||||
|
.markers{position:relative;height:11px}
|
||||||
|
.marker{
|
||||||
|
position:absolute;top:0;width:0;height:0;transform:translateX(-50%);transition:left .25s ease;
|
||||||
|
border-left:6px solid transparent;border-right:6px solid transparent;border-top:9px solid var(--ink);
|
||||||
|
}
|
||||||
|
.bar{display:flex;height:10px;border-radius:999px;overflow:hidden;background:var(--line)}
|
||||||
|
.bar div{height:100%}
|
||||||
|
.bar .s1{width:17.5%;background:var(--thin)}
|
||||||
|
.bar .s2{width:27.5%;background:var(--normal)}
|
||||||
|
.bar .s3{width:20%;background:var(--over)}
|
||||||
|
.bar .s4{width:35%;background:var(--obese)}
|
||||||
|
.legend{display:flex;flex-wrap:wrap;gap:6px 18px;margin-top:12px;font-size:12.5px;color:var(--muted)}
|
||||||
|
.legend i{display:inline-block;width:9px;height:9px;border-radius:3px;margin-right:6px}
|
||||||
|
|
||||||
|
.act{margin-top:20px;padding:12px 14px;background:#f8fafc;border:1px solid var(--line);border-radius:12px;font-size:13.5px;color:#374151;line-height:1.6}
|
||||||
|
.act b{color:var(--brand)}
|
||||||
|
|
||||||
|
/* ---- 热量结果 ---- */
|
||||||
|
.stats{display:grid;grid-template-columns:1fr 1fr;gap:12px}
|
||||||
|
@media(max-width:560px){.stats{grid-template-columns:1fr}}
|
||||||
|
.stat{background:#f8fafc;border:1px solid var(--line);border-radius:12px;padding:15px}
|
||||||
|
.stat .k{font-size:12.5px;font-weight:600;color:var(--muted)}
|
||||||
|
.stat .v{font-size:23px;font-weight:700;margin-top:5px;letter-spacing:-.5px}
|
||||||
|
.stat .v small{font-size:13px;font-weight:500;color:var(--muted);margin-left:3px}
|
||||||
|
.stat .n{font-size:12px;color:#9ca3af;margin-top:4px;line-height:1.5}
|
||||||
|
.stat.main{background:#eff6ff;border-color:#bfdbfe}
|
||||||
|
.stat.main .v{color:var(--brand)}
|
||||||
|
|
||||||
|
.notes{font-size:12.5px;color:var(--muted);line-height:1.8;margin:0;padding-left:18px}
|
||||||
|
.notes li{margin-bottom:2px}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="app">
|
||||||
|
|
||||||
|
<header>
|
||||||
|
<h1>BMI 与每日热量计算器</h1>
|
||||||
|
<p class="sub">输入身高、体重和日常活动量,自动算出 BMI、基础代谢与每日所需热量</p>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>你的数据</h2>
|
||||||
|
<div class="grid">
|
||||||
|
<div class="field">
|
||||||
|
<span class="lbl">性别</span>
|
||||||
|
<div class="seg">
|
||||||
|
<label><input type="radio" name="sex" value="male" checked><span>男</span></label>
|
||||||
|
<label><input type="radio" name="sex" value="female"><span>女</span></label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label class="lbl" for="age">年龄(岁)</label>
|
||||||
|
<input type="number" id="age" value="30" min="10" max="100" step="1">
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label class="lbl" for="height">身高(厘米)</label>
|
||||||
|
<input type="number" id="height" value="175" min="80" max="250" step="0.5">
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label class="lbl" for="weight">体重(千克)</label>
|
||||||
|
<input type="number" id="weight" value="60" min="20" max="300" step="0.1">
|
||||||
|
</div>
|
||||||
|
<div class="field full">
|
||||||
|
<label class="lbl" for="steps">每日平均步数</label>
|
||||||
|
<input type="number" id="steps" value="10000" min="0" max="50000" step="500">
|
||||||
|
<span class="hint">1 万步 ≈ 7 公里 ≈ 1.5 小时快走</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>BMI 结果</h2>
|
||||||
|
<div class="bmi-top">
|
||||||
|
<div class="bmi-num" id="bmiNum">--</div>
|
||||||
|
<div class="badge normal" id="bmiBadge">--</div>
|
||||||
|
</div>
|
||||||
|
<div class="scale">
|
||||||
|
<div class="markers"><div class="marker" id="marker" style="left:50%"></div></div>
|
||||||
|
<div class="bar">
|
||||||
|
<div class="s1"></div><div class="s2"></div><div class="s3"></div><div class="s4"></div>
|
||||||
|
</div>
|
||||||
|
<div class="legend">
|
||||||
|
<span><i style="background:var(--thin)"></i>偏瘦 < 18.5</span>
|
||||||
|
<span><i style="background:var(--normal)"></i>正常 18.5 – 23.9</span>
|
||||||
|
<span><i style="background:var(--over)"></i>超重 24 – 27.9</span>
|
||||||
|
<span><i style="background:var(--obese)"></i>肥胖 ≥ 28</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="act" id="actLine">活动等级:--</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>每日热量</h2>
|
||||||
|
<div class="stats">
|
||||||
|
<div class="stat">
|
||||||
|
<div class="k">基础代谢 BMR</div>
|
||||||
|
<div class="v" id="bmr">--<small>kcal</small></div>
|
||||||
|
<div class="n">躺着不动一天也会消耗的热量</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat main">
|
||||||
|
<div class="k">维持体重 TDEE</div>
|
||||||
|
<div class="v" id="tdee">--<small>kcal</small></div>
|
||||||
|
<div class="n">按你的日常活动量算出的每日总消耗</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="k">稳步增重(+10%)</div>
|
||||||
|
<div class="v" id="gain">--<small>kcal</small></div>
|
||||||
|
<div class="n">偏瘦想往上走,建议从这里开始</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="k">温和减脂(−15%)</div>
|
||||||
|
<div class="v" id="cut">--<small>kcal</small></div>
|
||||||
|
<div class="n">减重速度过快容易掉肌肉</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>说明</h2>
|
||||||
|
<ul class="notes">
|
||||||
|
<li>BMR 用 Mifflin-St Jeor 公式,比旧的 Harris-Benedict 公式更贴近现代人群。</li>
|
||||||
|
<li>活动系数按每日步数估算:<5千 1.20、5千–7.5千 1.375、7.5千–1万 1.55、≥1万 1.725。</li>
|
||||||
|
<li>BMI 分级采用中国成人标准(18.5 / 24 / 28)。</li>
|
||||||
|
<li>结果为估算值,个体差异约 ±10–15%;按此吃两周,体重没变化就把目标上下调 100–200 kcal。</li>
|
||||||
|
<li>如有代谢类疾病或正在服药,请以医生或营养师的意见为准。</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var STEP_TABLE = [
|
||||||
|
[5000, "久坐", 1.20],
|
||||||
|
[7500, "轻度活动", 1.375],
|
||||||
|
[10000, "中度活动", 1.55],
|
||||||
|
[Infinity, "高度活动", 1.725]
|
||||||
|
];
|
||||||
|
|
||||||
|
function palFromSteps(steps) {
|
||||||
|
for (var i = 0; i < STEP_TABLE.length; i++) {
|
||||||
|
if (steps < STEP_TABLE[i][0]) {
|
||||||
|
return { level: STEP_TABLE[i][1], pal: STEP_TABLE[i][2] };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { level: "高度活动", pal: 1.725 };
|
||||||
|
}
|
||||||
|
|
||||||
|
function classify(bmi) {
|
||||||
|
if (bmi < 18.5) return { label: "偏瘦", cls: "thin" };
|
||||||
|
if (bmi < 24) return { label: "正常", cls: "normal" };
|
||||||
|
if (bmi < 28) return { label: "超重", cls: "over" };
|
||||||
|
return { label: "肥胖", cls: "obese" };
|
||||||
|
}
|
||||||
|
|
||||||
|
function bmrOf(weight, height, age, sex) {
|
||||||
|
var base = 10 * weight + 6.25 * height - 5 * age;
|
||||||
|
return sex === "male" ? base + 5 : base - 161;
|
||||||
|
}
|
||||||
|
|
||||||
|
function num(id) {
|
||||||
|
var v = parseFloat(document.getElementById(id).value);
|
||||||
|
return isNaN(v) ? NaN : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearResults() {
|
||||||
|
document.getElementById("bmiNum").textContent = "--";
|
||||||
|
var badge = document.getElementById("bmiBadge");
|
||||||
|
badge.textContent = "--";
|
||||||
|
badge.className = "badge";
|
||||||
|
document.getElementById("bmr").innerHTML = "--<small>kcal</small>";
|
||||||
|
document.getElementById("tdee").innerHTML = "--<small>kcal</small>";
|
||||||
|
document.getElementById("gain").innerHTML = "--<small>kcal</small>";
|
||||||
|
document.getElementById("cut").innerHTML = "--<small>kcal</small>";
|
||||||
|
document.getElementById("actLine").textContent = "活动等级:--";
|
||||||
|
}
|
||||||
|
|
||||||
|
function update() {
|
||||||
|
var sex = document.querySelector('input[name="sex"]:checked').value;
|
||||||
|
var age = num("age"), h = num("height"), w = num("weight"), steps = num("steps");
|
||||||
|
|
||||||
|
if (!(age > 0) || !(h > 0) || !(w > 0) || isNaN(steps)) {
|
||||||
|
clearResults();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// BMI
|
||||||
|
var bmi = w / Math.pow(h / 100, 2);
|
||||||
|
var info = classify(bmi);
|
||||||
|
document.getElementById("bmiNum").textContent = bmi.toFixed(1);
|
||||||
|
var badge = document.getElementById("bmiBadge");
|
||||||
|
badge.textContent = info.label;
|
||||||
|
badge.className = "badge " + info.cls;
|
||||||
|
|
||||||
|
// 刻度指针:BMI 15–35 映射到 0–100%
|
||||||
|
var clamped = Math.min(35, Math.max(15, bmi));
|
||||||
|
document.getElementById("marker").style.left = ((clamped - 15) / 20 * 100).toFixed(2) + "%";
|
||||||
|
|
||||||
|
// 热量
|
||||||
|
var bmr = bmrOf(w, h, age, sex);
|
||||||
|
var act = palFromSteps(steps);
|
||||||
|
var tdee = bmr * act.pal;
|
||||||
|
|
||||||
|
document.getElementById("bmr").innerHTML = Math.round(bmr) + '<small>kcal</small>';
|
||||||
|
document.getElementById("tdee").innerHTML = Math.round(tdee) + '<small>kcal</small>';
|
||||||
|
document.getElementById("gain").innerHTML = Math.round(tdee * 1.10) + '<small>kcal</small>';
|
||||||
|
document.getElementById("cut").innerHTML = Math.round(tdee * 0.85) + '<small>kcal</small>';
|
||||||
|
document.getElementById("actLine").innerHTML =
|
||||||
|
"活动等级:<b>" + act.level + "</b>(PAL " + act.pal + ")· 步数 " +
|
||||||
|
Math.round(steps).toLocaleString() + " 步/天";
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById("age").addEventListener("input", update);
|
||||||
|
document.getElementById("height").addEventListener("input", update);
|
||||||
|
document.getElementById("weight").addEventListener("input", update);
|
||||||
|
document.getElementById("steps").addEventListener("input", update);
|
||||||
|
var radios = document.querySelectorAll('input[name="sex"]');
|
||||||
|
for (var i = 0; i < radios.length; i++) radios[i].addEventListener("change", update);
|
||||||
|
|
||||||
|
update();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user