Files
daily-calorie-calculator/index.html
T

291 lines
11 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
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.0" />
<title>每日热量需求计算器 | BMI · BMR · PAL · TDEE</title>
<style>
:root {
--bg: #f4f7fb;
--card: #ffffff;
--pri: #2f6fed;
--pri-dark: #1f4fb8;
--txt: #1f2937;
--muted: #6b7280;
--line: #e5e7eb;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: -apple-system, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
background: var(--bg);
color: var(--txt);
line-height: 1.6;
}
.wrap { max-width: 720px; margin: 0 auto; padding: 28px 16px 64px; }
h1 { font-size: 22px; margin: 0 0 6px; }
.sub { color: var(--muted); font-size: 14px; margin-bottom: 20px; }
.card {
background: var(--card);
border-radius: 14px;
padding: 20px;
box-shadow: 0 1px 3px rgba(15, 23, 42, .06), 0 10px 28px rgba(15, 23, 42, .05);
margin-bottom: 16px;
}
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.full { grid-column: 1 / -1; }
label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
input, select {
width: 100%;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 9px;
font-size: 15px;
background: #fff;
color: var(--txt);
font-family: inherit;
}
input:focus, select:focus {
outline: none;
border-color: var(--pri);
box-shadow: 0 0 0 3px rgba(47, 111, 237, .15);
}
button {
width: 100%;
margin-top: 18px;
padding: 13px;
border: none;
border-radius: 10px;
background: var(--pri);
color: #fff;
font-size: 16px;
font-weight: 600;
cursor: pointer;
font-family: inherit;
}
button:hover { background: var(--pri-dark); }
.note { font-size: 12.5px; color: var(--muted); margin-top: 10px; min-height: 18px; }
.result { display: none; }
.hero { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.hero .big { font-size: 38px; font-weight: 800; color: var(--pri); line-height: 1.1; }
.hero .cap { font-size: 13px; color: var(--muted); }
.tag { display: inline-block; padding: 3px 12px; border-radius: 999px; font-size: 12.5px; font-weight: 700; }
.tag.thin { background: #dbeafe; color: #1e40af; }
.tag.ok { background: #dcfce7; color: #166534; }
.tag.over { background: #fef3c7; color: #92400e; }
.tag.obese { background: #fee2e2; color: #991b1b; }
.metric { display: flex; justify-content: space-between; align-items: baseline; padding: 9px 0; border-bottom: 1px dashed var(--line); }
.metric:last-child { border-bottom: none; }
.metric .k { color: var(--muted); font-size: 14px; }
.metric .v { font-weight: 700; font-size: 16px; }
h2 { font-size: 15px; margin: 4px 0 12px; }
.plans { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.plan { border: 1px solid var(--line); border-radius: 10px; padding: 12px 8px; text-align: center; }
.plan.active { border-color: var(--pri); background: #f0f5ff; }
.plan .t { font-size: 13px; color: var(--muted); }
.plan .n { font-size: 19px; font-weight: 800; }
.plan .u { font-size: 11px; color: var(--muted); }
.bar { display: flex; height: 10px; border-radius: 999px; overflow: hidden; margin: 14px 0 6px; }
.bar span { display: block; }
.b1 { background: #93c5fd; width: 18.5%; }
.b2 { background: #86efac; width: 5.5%; }
.b3 { background: #fcd34d; width: 4%; }
.b4 { background: #fca5a5; flex: 1; }
.scale { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--muted); }
@media (max-width: 520px) {
.grid, .plans { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<div class="wrap">
<h1>每日热量需求计算器</h1>
<div class="sub">输入身高体重与日常活动量,估算 BMI、BMR、PAL 与 TDEE,并给出每日建议摄入热量。</div>
<div class="card">
<div class="grid">
<div>
<label for="sex">性别</label>
<select id="sex">
<option value="female">女</option>
<option value="male">男</option>
</select>
</div>
<div>
<label for="age">年龄(岁)</label>
<input id="age" type="number" min="10" max="100" step="1" value="25" />
</div>
<div>
<label for="height">身高(cm)</label>
<input id="height" type="number" min="100" max="230" step="0.1" placeholder="例如 160" />
</div>
<div>
<label for="weight">体重(kg)</label>
<input id="weight" type="number" min="25" max="200" step="0.1" placeholder="例如 48" />
</div>
<div class="full">
<label for="pal">日常活动量</label>
<select id="pal">
<option value="1.2" selected>几乎没有 / 久坐(基本不运动,以坐为主)</option>
<option value="1.375">轻度活动(每周 1–3 次轻量运动或经常散步)</option>
<option value="1.55">中度活动(每周 3–5 次中等强度运动)</option>
<option value="1.725">高度活动(每周 6–7 次高强度运动)</option>
<option value="1.9">极高活动(体力劳动或一天两练)</option>
</select>
</div>
</div>
<button id="calc">计算</button>
<div class="note" id="msg"></div>
</div>
<div class="card result" id="result">
<div class="hero">
<div>
<div class="cap">你的 BMI</div>
<div class="big" id="bmiVal">–</div>
</div>
<div style="text-align:right">
<div class="cap">体型判定(中国成人标准)</div>
<div id="bmiTag" class="tag ok" style="margin-top:6px">正常</div>
</div>
</div>
<div class="bar">
<span class="b1"></span><span class="b2"></span><span class="b3"></span><span class="b4"></span>
</div>
<div class="scale">
<span>偏瘦 18.5</span><span>正常 24</span><span>超重 28</span><span>肥胖</span>
</div>
<div style="margin-top:16px">
<div class="metric"><span class="k">基础代谢 BMR(Mifflin-St Jeor)</span><span class="v" id="bmrVal">–</span></div>
<div class="metric"><span class="k">活动系数 PAL</span><span class="v" id="palVal">–</span></div>
<div class="metric"><span class="k">每日总消耗 TDEE = BMR × PAL</span><span class="v" id="tdeeVal">–</span></div>
</div>
<h2 style="margin-top:20px">每日建议摄入热量</h2>
<div class="plans">
<div class="plan" id="planCut">
<div class="t">减脂</div>
<div class="n" id="cutVal">–</div>
<div class="u">kcal / 天</div>
</div>
<div class="plan" id="planKeep">
<div class="t">维持体重</div>
<div class="n" id="keepVal">–</div>
<div class="u">kcal / 天</div>
</div>
<div class="plan" id="planGain">
<div class="t">增重</div>
<div class="n" id="gainVal">–</div>
<div class="u">kcal / 天</div>
</div>
</div>
<div class="note" id="advice"></div>
<div class="metric" style="margin-top:16px"><span class="k">蛋白质参考(1.2 g/kg)</span><span class="v" id="protVal">–</span></div>
<div class="metric"><span class="k">饮水参考(30 ml/kg)</span><span class="v" id="waterVal">–</span></div>
<div class="note">
说明:以上为估算值,个体差异约 ±10%。公式:BMR(男)= 10×体重 + 6.25×身高 − 5×年龄 + 5;
BMR(女)= 10×体重 + 6.25×身高 − 5×年龄 − 161;TDEE = BMR × PAL。BMI 分级采用中国成人标准
(&lt;18.5 偏瘦,18.5–23.9 正常,24–27.9 超重,≥28 肥胖)。如有疾病或特殊状况,请咨询医生或注册营养师。
</div>
</div>
</div>
<script>
(function () {
var $ = function (id) { return document.getElementById(id); };
function round(n) { return Math.round(n); }
function classify(bmi) {
if (bmi < 18.5) return { label: "偏瘦", cls: "thin" };
if (bmi < 24) return { label: "正常", cls: "ok" };
if (bmi < 28) return { label: "超重", cls: "over" };
return { label: "肥胖", cls: "obese" };
}
function bmrOf(sex, weightKg, heightCm, age) {
var base = 10 * weightKg + 6.25 * heightCm - 5 * age;
return sex === "male" ? base + 5 : base - 161;
}
function calc() {
var sex = $("sex").value;
var age = parseFloat($("age").value);
var h = parseFloat($("height").value);
var w = parseFloat($("weight").value);
var pal = parseFloat($("pal").value);
var msg = $("msg");
if (!(age > 0) || !(h > 0) || !(w > 0)) {
msg.textContent = "请填写有效的年龄、身高与体重。";
$("result").style.display = "none";
return;
}
msg.textContent = "";
var bmi = w / Math.pow(h / 100, 2);
var bmr = bmrOf(sex, w, h, age);
var tdee = bmr * pal;
var c = classify(bmi);
var cut = tdee - 400;
var keep = tdee;
var gain = tdee + 300;
$("bmiVal").textContent = bmi.toFixed(1);
var tag = $("bmiTag");
tag.textContent = c.label;
tag.className = "tag " + c.cls;
$("bmrVal").textContent = round(bmr) + " kcal";
$("palVal").textContent = pal.toFixed(3);
$("tdeeVal").textContent = round(tdee) + " kcal";
$("cutVal").textContent = round(cut);
$("keepVal").textContent = round(keep);
$("gainVal").textContent = round(gain);
$("planCut").className = "plan";
$("planKeep").className = "plan";
$("planGain").className = "plan";
var advice = "";
if (bmi < 18.5) {
$("planGain").className = "plan active";
advice = "你的 BMI 低于 18.5(偏瘦),建议以「增重」为目标,在 TDEE 基础上每天多吃约 300 kcal,并保证优质蛋白与力量训练。";
} else if (bmi < 19.5) {
$("planGain").className = "plan active";
advice = "BMI 虽在正常范围,但已贴近偏瘦边缘,建议按「增重 / 维持偏上」来吃(TDEE ~ TDEE+300 kcal),避免继续掉秤。";
} else if (bmi < 24) {
$("planKeep").className = "plan active";
advice = "BMI 处于正常范围,按「维持体重」摄入即可(约等于 TDEE)。";
} else if (bmi < 28) {
$("planCut").className = "plan active";
advice = "BMI 属于超重,建议以「减脂」为目标(TDEE 减 300–500 kcal),并配合运动。";
} else {
$("planCut").className = "plan active";
advice = "BMI 属于肥胖,建议在专业指导下控制热量并增加运动。";
}
$("advice").textContent = advice;
$("protVal").textContent = (w * 1.2).toFixed(0) + " g";
$("waterVal").textContent = (w * 30 / 1000).toFixed(1) + " L";
$("result").style.display = "block";
}
$("calc").addEventListener("click", calc);
["age", "height", "weight", "sex", "pal"].forEach(function (id) {
$(id).addEventListener("change", function () {
if ($("result").style.display === "block") calc();
});
});
})();
</script>
</body>
</html>