feat: 热量计算器加入营养方面内容 (tdee-calculator.html)
本次所做的决策: 1. 需求:在原有「按日常活动量(每天约 1 万步)计算一天需要摄入的热量」的基础上,加入营养方面的内容。 2. 存放位置:仓库根目录 tdee-calculator.html(在原有文件上追加,不新建文件)。
This commit is contained in:
+37
-3
@@ -17,12 +17,13 @@
|
||||
input, select { width: 100%; padding: 10px 12px; border: 1px solid #cfd8e3; border-radius: 8px; font-size: 1rem; background: #fff; }
|
||||
input:focus, select:focus { outline: none; border-color: #2f80ed; box-shadow: 0 0 0 3px rgba(47,128,237,.12); }
|
||||
.results { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
|
||||
@media (max-width: 520px) { .results { grid-template-columns: 1fr; } }
|
||||
.results.three { grid-template-columns: repeat(3, 1fr); }
|
||||
@media (max-width: 520px) { .results, .results.three { grid-template-columns: 1fr; } }
|
||||
.metric { background: #f8fafc; border: 1px solid #e8eef5; border-radius: 10px; padding: 14px; }
|
||||
.metric .k { font-size: .8rem; color: #5b6b7b; }
|
||||
.metric .v { font-size: 1.5rem; font-weight: 700; margin-top: 2px; }
|
||||
.metric .u { font-size: .85rem; font-weight: 400; color: #5b6b7b; }
|
||||
.hint { font-size: .8rem; color: #8494a4; margin-top: 10px; }
|
||||
.hint { font-size: .8rem; color: #8494a4; margin-top: 6px; }
|
||||
h2 { font-size: 1.05rem; margin: 0 0 12px; }
|
||||
ol { padding-left: 1.2rem; margin: 0; }
|
||||
li { margin-bottom: 6px; }
|
||||
@@ -33,7 +34,7 @@
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<h1>每日热量需求计算器</h1>
|
||||
<p class="sub">根据你的身体数据与日常活动量(步数),估算维持体重所需热量,并给出增重 / 减脂目标。</p>
|
||||
<p class="sub">根据你的身体数据与日常活动量(步数),估算维持体重所需热量,并给出增重 / 减脂目标与每日营养摄入建议。</p>
|
||||
|
||||
<div class="card">
|
||||
<div class="grid">
|
||||
@@ -74,12 +75,23 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>营养摄入建议</h2>
|
||||
<p class="hint" style="margin-top: 0;">以下按「维持热量」估算每日宏量营养素摄入量。</p>
|
||||
<div class="results three">
|
||||
<div class="metric"><div class="k">蛋白质</div><div class="v" id="protein">--<span class="u"> g</span></div><div class="hint" id="proteinKcal">--</div></div>
|
||||
<div class="metric"><div class="k">脂肪</div><div class="v" id="fat">--<span class="u"> g</span></div><div class="hint" id="fatKcal">--</div></div>
|
||||
<div class="metric"><div class="k">碳水化合物</div><div class="v" id="carb">--<span class="u"> g</span></div><div class="hint" id="carbKcal">--</div></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>算法说明</h2>
|
||||
<ol>
|
||||
<li><b>基础代谢(BMR)</b>:采用 Mifflin-St Jeor 公式。男性 <code>10×体重 + 6.25×身高 − 5×年龄 + 5</code>;女性 <code>10×体重 + 6.25×身高 − 5×年龄 − 161</code>(体重 kg、身高 cm、年龄 岁)。</li>
|
||||
<li><b>维持热量(TDEE)</b>:在 BMR 基础上叠加日常活动消耗。久坐基础系数取 1.2,再按步数追加:<code>TDEE = BMR × 1.2 + 步数 × 体重 × 0.00057</code>。</li>
|
||||
<li><b>目标热量</b>:按约 7700 kcal ≈ 1 kg 估算,增重取 +300、减脂取 −500。</li>
|
||||
<li><b>营养分配</b>:蛋白质按 1.4 g/kg 体重(参考区间 1.2–1.6 g/kg,1 g ≈ 4 kcal);脂肪占总热量 25%(1 g ≈ 9 kcal);剩余热量分配给碳水化合物(1 g ≈ 4 kcal)。</li>
|
||||
</ol>
|
||||
<p class="foot">结果仅供参考,不构成医疗建议。</p>
|
||||
</div>
|
||||
@@ -90,6 +102,14 @@
|
||||
var fields = ["sex", "age", "height", "weight", "steps"];
|
||||
function num(v) { var n = parseFloat(v); return isFinite(n) ? n : NaN; }
|
||||
function fmt(x) { return Math.round(x).toLocaleString("zh-CN") + '<span class="u"> kcal</span>'; }
|
||||
function clearNutrition() {
|
||||
$("protein").innerHTML = '--<span class="u"> g</span>';
|
||||
$("fat").innerHTML = '--<span class="u"> g</span>';
|
||||
$("carb").innerHTML = '--<span class="u"> g</span>';
|
||||
$("proteinKcal").textContent = '--';
|
||||
$("fatKcal").textContent = '--';
|
||||
$("carbKcal").textContent = '--';
|
||||
}
|
||||
function calc() {
|
||||
var sex = $("sex").value;
|
||||
var age = num($("age").value);
|
||||
@@ -99,6 +119,7 @@
|
||||
var valid = age > 0 && h > 0 && w > 0 && steps >= 0 && isFinite(steps);
|
||||
if (!valid) {
|
||||
["bmr", "tdee", "gain", "cut"].forEach(function (id) { $(id).innerHTML = '--<span class="u"> kcal</span>'; });
|
||||
clearNutrition();
|
||||
return;
|
||||
}
|
||||
var bmr = 10 * w + 6.25 * h - 5 * age + (sex === "male" ? 5 : -161);
|
||||
@@ -107,6 +128,19 @@
|
||||
$("tdee").innerHTML = fmt(tdee);
|
||||
$("gain").innerHTML = fmt(tdee + 300);
|
||||
$("cut").innerHTML = fmt(tdee - 500);
|
||||
|
||||
var proteinG = 1.4 * w;
|
||||
var proteinKcal = proteinG * 4;
|
||||
var fatKcal = tdee * 0.25;
|
||||
var fatG = fatKcal / 9;
|
||||
var carbKcal = Math.max(tdee - proteinKcal - fatKcal, 0);
|
||||
var carbG = carbKcal / 4;
|
||||
$("protein").innerHTML = Math.round(proteinG) + '<span class="u"> g</span>';
|
||||
$("fat").innerHTML = Math.round(fatG) + '<span class="u"> g</span>';
|
||||
$("carb").innerHTML = Math.round(carbG) + '<span class="u"> g</span>';
|
||||
$("proteinKcal").textContent = Math.round(proteinKcal) + " kcal(1.4 g/kg)";
|
||||
$("fatKcal").textContent = Math.round(fatKcal) + " kcal(占总热量 25%)";
|
||||
$("carbKcal").textContent = Math.round(carbKcal) + " kcal(剩余热量)";
|
||||
}
|
||||
fields.forEach(function (f) {
|
||||
$(f).addEventListener("input", calc);
|
||||
|
||||
Reference in New Issue
Block a user