在原热量计算器基础上增加今日饮食记录功能
- 在原有身体参数与每日目标计算之外,改为按今日所吃食物输入,输出摄入热量 - 收录常见食物每 100 g 的热量与蛋白质、脂肪、碳水化合物、膳食纤维含量 - 以克数换算每条记录,实时汇总今日摄入热量与四大营养素 - 将摄入汇总与每日目标热量、三大产能营养素目标对比,显示剩余或超出 - 支持未收录食物的自定义每 100 g 数值录入,记录保存在本机浏览器
This commit is contained in:
+378
-53
@@ -11,7 +11,7 @@
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||
background: #f4f6f8; color: #1f2933; line-height: 1.6;
|
||||
}
|
||||
.wrap { max-width: 860px; margin: 0 auto; }
|
||||
.wrap { max-width: 880px; margin: 0 auto; }
|
||||
h1 { font-size: 22px; margin: 0 0 4px; }
|
||||
.sub { color: #6b7280; font-size: 13px; margin-bottom: 20px; }
|
||||
.card {
|
||||
@@ -21,9 +21,9 @@
|
||||
.card h2 { font-size: 15px; margin: 0 0 14px; color: #0f766e; letter-spacing: .5px; }
|
||||
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 14px; }
|
||||
label { display: block; font-size: 12px; color: #6b7280; margin-bottom: 4px; }
|
||||
input[type=number], select {
|
||||
input[type=number], input[type=text], select {
|
||||
width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 8px;
|
||||
font-size: 14px; background: #fff; color: #1f2933;
|
||||
font-size: 14px; background: #fff; color: #1f2933; font-family: inherit;
|
||||
}
|
||||
input:focus, select:focus { outline: 2px solid #0d9488; outline-offset: -1px; border-color: #0d9488; }
|
||||
.radios { display: flex; gap: 16px; padding-top: 4px; }
|
||||
@@ -36,9 +36,7 @@
|
||||
.hero .unit { font-size: 16px; font-weight: 400; opacity: .9; }
|
||||
.hero .desc { font-size: 13px; opacity: .9; margin-top: 6px; }
|
||||
.chips { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
|
||||
.chip {
|
||||
background: rgba(255,255,255,.16); border-radius: 8px; padding: 8px 12px; font-size: 13px;
|
||||
}
|
||||
.chip { background: rgba(255,255,255,.16); border-radius: 8px; padding: 8px 12px; font-size: 13px; }
|
||||
.chip b { font-size: 16px; display: block; font-weight: 600; }
|
||||
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
|
||||
th, td { padding: 9px 8px; text-align: left; border-bottom: 1px solid #eef1f4; }
|
||||
@@ -54,15 +52,52 @@
|
||||
.note { font-size: 12.5px; color: #6b7280; margin-top: 14px; line-height: 1.7; }
|
||||
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 0 28px; }
|
||||
@media (max-width: 560px) { .cols { grid-template-columns: 1fr; } }
|
||||
|
||||
/* 饮食记录模块 */
|
||||
.food-input { display: grid; grid-template-columns: 1fr 110px 96px; gap: 10px; align-items: end; }
|
||||
@media (max-width: 560px) { .food-input { grid-template-columns: 1fr; } }
|
||||
.btn {
|
||||
padding: 9px 16px; border: none; border-radius: 8px; font-size: 14px;
|
||||
background: #0d9488; color: #fff; cursor: pointer; font-family: inherit; font-weight: 500;
|
||||
}
|
||||
.btn:hover { background: #0f766e; }
|
||||
.btn-ghost { background: #eef2f3; color: #475b63; }
|
||||
.btn-ghost:hover { background: #e2e8ea; }
|
||||
.btn-line { background: none; border: none; color: #b91c1c; cursor: pointer; font-size: 13px; padding: 4px 6px; font-family: inherit; }
|
||||
.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; flex-wrap: wrap; gap: 10px; }
|
||||
.msg { font-size: 12.5px; margin-top: 8px; min-height: 18px; }
|
||||
.msg-err { color: #b91c1c; }
|
||||
.msg-ok { color: #0f766e; }
|
||||
.progress { height: 12px; background: #e8edf0; border-radius: 99px; overflow: hidden; margin: 12px 0 8px; }
|
||||
.progress i { display: block; height: 100%; background: linear-gradient(90deg, #14b8a6, #0d9488); transition: width .25s; }
|
||||
.progress i.over { background: linear-gradient(90deg, #f59e0b, #dc2626); }
|
||||
.prog-label { display: flex; justify-content: space-between; font-size: 12.5px; color: #4b5563; font-variant-numeric: tabular-nums; }
|
||||
.empty { color: #9aa5ad; font-size: 13px; text-align: center; padding: 18px 0; }
|
||||
tfoot td { font-weight: 600; background: #fafbfc; border-bottom: none; }
|
||||
.hint { font-size: 12px; color: #9aa5ad; margin-top: 6px; }
|
||||
.add-intake { color: #0f766e; }
|
||||
.over-text { color: #b91c1c; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<h1>每日热量与七大营养素计算器</h1>
|
||||
<div class="sub">算法:Mifflin-St Jeor 基础代谢 + MET 法步行能耗 + DRIs 2023 供能比</div>
|
||||
<div class="sub">算法:Mifflin-St Jeor 基础代谢 + MET 法步行能耗 + DRIs 2023 供能比 + 食物成分表折算</div>
|
||||
|
||||
<div class="hero">
|
||||
<div class="desc">每日建议摄入热量</div>
|
||||
<div class="big"><span id="targetKcal">1800</span> <span class="unit">kcal / 天</span></div>
|
||||
<div class="chips">
|
||||
<div class="chip">BMI<b id="bmi">18.7</b></div>
|
||||
<div class="chip">基础代谢 BMR<b id="bmr">1230</b></div>
|
||||
<div class="chip">步行能耗<b id="walk">300</b></div>
|
||||
<div class="chip">总消耗 TDEE<b id="tdee">1776</b></div>
|
||||
<div class="chip">今日已摄入<b id="intakeChip">0</b></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>① 输入参数</h2>
|
||||
<h2>① 身体参数与每日目标</h2>
|
||||
<div class="grid">
|
||||
<div>
|
||||
<label>性别</label>
|
||||
@@ -79,7 +114,7 @@
|
||||
<label>目标</label>
|
||||
<select id="goal">
|
||||
<option value="0" selected>维持体重</option>
|
||||
<option value="200">小幅度增重(+200 kcal)</option>
|
||||
<option value="200">小幅增重(+200 kcal)</option>
|
||||
<option value="300">增重(+300 kcal)</option>
|
||||
<option value="-300">减脂(−300 kcal)</option>
|
||||
</select>
|
||||
@@ -87,19 +122,63 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="hero">
|
||||
<div class="desc">每日建议摄入热量</div>
|
||||
<div class="big"><span id="targetKcal">1800</span> <span class="unit">kcal / 天</span></div>
|
||||
<div class="chips">
|
||||
<div class="chip">BMI<b id="bmi">18.7</b></div>
|
||||
<div class="chip">基础代谢 BMR<b id="bmr">1230</b></div>
|
||||
<div class="chip">步行能耗<b id="walk">300</b></div>
|
||||
<div class="chip">总消耗 TDEE<b id="tdee">1776</b></div>
|
||||
<div class="card">
|
||||
<h2>② 今日饮食记录 —— 输入你吃了什么,输出热量</h2>
|
||||
|
||||
<div class="food-input">
|
||||
<div>
|
||||
<label>食物名称(可直接输入,自动匹配)</label>
|
||||
<input type="text" id="foodInput" list="foodList" placeholder="例如:米饭、鸡胸肉、苹果……">
|
||||
<datalist id="foodList"></datalist>
|
||||
</div>
|
||||
<div>
|
||||
<label>分量(g)</label>
|
||||
<input type="number" id="foodGrams" value="100" min="1" max="3000" step="1">
|
||||
</div>
|
||||
<button class="btn" id="addBtn">添加到今日</button>
|
||||
</div>
|
||||
<div class="hint">参考分量:一碗米饭约 150 g,一个鸡蛋约 50 g,一个苹果约 200 g,一杯牛奶约 250 g,一勺植物油约 10 g。</div>
|
||||
<div class="msg" id="foodMsg"></div>
|
||||
|
||||
<table style="margin-top:14px">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>食物</th><th class="num">分量(g)</th><th class="num">热量</th>
|
||||
<th class="num">蛋白</th><th class="num">脂肪</th><th class="num">碳水</th><th class="num">纤维</th><th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="mealBody"></tbody>
|
||||
<tfoot id="mealFoot"></tfoot>
|
||||
</table>
|
||||
|
||||
<div class="progress"><i id="progFill" style="width:0%"></i></div>
|
||||
<div class="prog-label">
|
||||
<span>已摄入 <b id="intakeKcal">0</b> kcal / 目标 <b id="targetKcal2">1800</b> kcal</span>
|
||||
<span id="remainLabel">剩余 1800 kcal</span>
|
||||
</div>
|
||||
|
||||
<div class="toolbar" style="margin-top:16px">
|
||||
<span class="note" style="margin:0">记录自动保存在本机浏览器中,刷新不丢失。</span>
|
||||
<button class="btn btn-ghost" id="clearBtn">清空今日记录</button>
|
||||
</div>
|
||||
|
||||
<details style="margin-top:14px">
|
||||
<summary style="cursor:pointer;font-size:13.5px;color:#0f766e">食物库没有我要的?点这里自定义添加</summary>
|
||||
<div class="grid" style="margin-top:14px">
|
||||
<div><label>食物名称</label><input type="text" id="cName" placeholder="自定义名称"></div>
|
||||
<div><label>热量(kcal/100g)</label><input type="number" id="cKcal" value="0" min="0" step="0.1"></div>
|
||||
<div><label>蛋白质(g/100g)</label><input type="number" id="cP" value="0" min="0" step="0.1"></div>
|
||||
<div><label>脂肪(g/100g)</label><input type="number" id="cF" value="0" min="0" step="0.1"></div>
|
||||
<div><label>碳水(g/100g)</label><input type="number" id="cC" value="0" min="0" step="0.1"></div>
|
||||
<div><label>膳食纤维(g/100g)</label><input type="number" id="cFib" value="0" min="0" step="0.1"></div>
|
||||
<div><label>本次分量(g)</label><input type="number" id="cGrams" value="100" min="1" step="1"></div>
|
||||
<div style="display:flex;align-items:flex-end"><button class="btn btn-ghost" id="addCustomBtn" style="width:100%">加入今日</button></div>
|
||||
</div>
|
||||
</details>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>② 三大产能营养素分配</h2>
|
||||
<h2>③ 三大产能营养素:目标 / 已摄入 / 剩余</h2>
|
||||
<div class="macro-bar">
|
||||
<span id="barP" style="background:#0d9488"></span>
|
||||
<span id="barF" style="background:#f59e0b"></span>
|
||||
@@ -112,14 +191,14 @@
|
||||
</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>营养素</th><th class="num">每日推荐</th><th class="num">能量系数</th><th class="num">提供能量</th><th class="num">供能比</th></tr>
|
||||
<tr><th>营养素</th><th class="num">目标</th><th class="num">能量系数</th><th class="num">目标能量</th><th class="num">已摄入</th><th class="num">剩余</th></tr>
|
||||
</thead>
|
||||
<tbody id="macroBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>③ 七大营养素全景</h2>
|
||||
<h2>④ 七大营养素全景</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>#</th><th>营养素</th><th>是否产能</th><th class="num">每日推荐量</th><th class="num">能量</th></tr>
|
||||
@@ -128,12 +207,12 @@
|
||||
</table>
|
||||
<div class="note">
|
||||
蛋白质/脂肪/碳水为产能营养素;膳食纤维 25–30 g 建议量,能量按 2 kcal/g 计(通常并入碳水账目);<br>
|
||||
维生素、矿物质、水不产能,其参考摄入量见下表。
|
||||
维生素、矿物质、水不产能,其参考摄入量见下。
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>④ 微量营养素参考摄入量</h2>
|
||||
<h2>⑤ 微量营养素参考摄入量</h2>
|
||||
<div class="cols">
|
||||
<table>
|
||||
<thead><tr><th>维生素</th><th class="num">每日量</th></tr></thead>
|
||||
@@ -168,12 +247,82 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="note">以上为成年女性 18–49 岁、轻身体活动水平参考值;男性或孕期/哺乳期需上调。公式仅算存在 ±10%~15% 个体差异,建议以 2–4 周体重变化实测校准。</div>
|
||||
<div class="note">以上为成年女性 18–49 岁、轻身体活动水平参考值;男性或孕期/哺乳期需上调。公式估算存在 ±10%~15% 个体差异,建议以 2–4 周体重变化实测校准。</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
/* ============ 一、食物成分表(每 100 g 可食部:热量kcal, 蛋白g, 脂肪g, 碳水g, 纤维g) ============ */
|
||||
const DB = {
|
||||
"米饭(蒸熟)": [116, 2.6, 0.3, 25.9, 0.3],
|
||||
"白粥": [46, 1.1, 0.3, 9.9, 0.1],
|
||||
"馒头": [223, 7.0, 1.1, 47.0, 1.3],
|
||||
"面条(煮熟)": [110, 2.7, 0.2, 24.3, 0.4],
|
||||
"全麦面包": [246, 8.8, 3.3, 45.0, 6.0],
|
||||
"吐司面包": [313, 8.3, 5.1, 58.6, 2.7],
|
||||
"燕麦片": [377, 15.0, 6.7, 61.0, 10.6],
|
||||
"玉米(鲜)": [112, 4.0, 1.2, 22.8, 2.9],
|
||||
"红薯(蒸)": [90, 1.6, 0.2, 20.7, 1.6],
|
||||
"土豆(蒸)": [81, 2.0, 0.2, 17.8, 1.2],
|
||||
"鸡胸肉": [133, 19.4, 5.0, 2.5, 0],
|
||||
"鸡腿肉": [181, 16.0, 13.0, 0, 0],
|
||||
"瘦猪肉": [143, 20.3, 6.2, 1.5, 0],
|
||||
"五花肉": [508, 7.7, 53.0, 2.4, 0],
|
||||
"牛肉(瘦)": [106, 20.2, 2.3, 1.2, 0],
|
||||
"羊肉(瘦)": [118, 20.5, 3.9, 0.2, 0],
|
||||
"鸡蛋": [144, 13.3, 8.8, 2.8, 0],
|
||||
"鸭蛋": [180, 12.6, 13.0, 3.1, 0],
|
||||
"火腿肠": [212, 14.0, 10.4, 15.6, 0],
|
||||
"三文鱼": [139, 17.2, 7.8, 0, 0],
|
||||
"带鱼": [127, 17.7, 4.9, 3.1, 0],
|
||||
"草鱼": [113, 16.6, 5.2, 0, 0],
|
||||
"虾": [93, 18.6, 0.8, 2.8, 0],
|
||||
"北豆腐": [116, 12.2, 4.8, 4.2, 0.5],
|
||||
"南豆腐": [87, 6.2, 2.5, 4.8, 0.4],
|
||||
"豆浆": [31, 3.0, 1.6, 1.2, 1.1],
|
||||
"牛奶(全脂)": [65, 3.3, 3.6, 4.9, 0],
|
||||
"酸奶": [72, 2.5, 2.7, 9.3, 0],
|
||||
"奶酪": [328, 25.7, 23.5, 3.5, 0],
|
||||
"黄豆": [390, 35.0, 16.0, 34.2, 15.5],
|
||||
"西兰花": [36, 4.1, 0.6, 4.3, 1.6],
|
||||
"菠菜": [28, 2.6, 0.3, 4.5, 1.7],
|
||||
"生菜": [15, 1.3, 0.3, 2.0, 0.7],
|
||||
"大白菜": [20, 1.5, 0.1, 3.2, 0.8],
|
||||
"番茄": [20, 0.9, 0.2, 4.0, 0.5],
|
||||
"黄瓜": [16, 0.8, 0.2, 2.9, 0.5],
|
||||
"胡萝卜": [39, 1.0, 0.2, 8.8, 1.1],
|
||||
"茄子": [23, 1.1, 0.2, 4.9, 1.3],
|
||||
"青椒": [22, 1.4, 0.3, 5.4, 1.4],
|
||||
"蘑菇(鲜)": [24, 2.7, 0.1, 4.1, 2.1],
|
||||
"木耳(干)": [265, 12.1, 1.5, 65.6, 29.9],
|
||||
"苹果": [53, 0.2, 0.2, 13.5, 2.4],
|
||||
"香蕉": [93, 1.4, 0.2, 22.0, 1.2],
|
||||
"橙子": [48, 0.8, 0.2, 11.1, 0.6],
|
||||
"葡萄": [45, 0.5, 0.2, 10.3, 0.4],
|
||||
"西瓜": [31, 0.5, 0.3, 7.0, 0.2],
|
||||
"草莓": [32, 1.0, 0.2, 7.1, 1.1],
|
||||
"梨": [51, 0.4, 0.2, 13.3, 3.1],
|
||||
"猕猴桃": [61, 0.8, 0.6, 14.5, 2.6],
|
||||
"蓝莓": [57, 0.7, 0.3, 14.5, 2.4],
|
||||
"花生(炒)": [601, 21.7, 48.0, 23.8, 6.3],
|
||||
"核桃": [646, 14.9, 58.8, 19.1, 9.5],
|
||||
"杏仁": [578, 22.5, 45.4, 27.5, 8.0],
|
||||
"植物油": [899, 0, 99.9, 0, 0],
|
||||
"橄榄油": [899, 0, 99.9, 0, 0],
|
||||
"黄油": [888, 1.4, 98.0, 0, 0],
|
||||
"可乐": [43, 0, 0, 10.8, 0],
|
||||
"啤酒": [32, 0.4, 0, 4.6, 0],
|
||||
"奶茶(全糖)": [90, 1.0, 2.5, 15.0, 0],
|
||||
"黑咖啡": [2, 0.2, 0, 0.3, 0],
|
||||
"白砂糖": [400, 0, 0, 99.9, 0],
|
||||
"牛奶巧克力": [586, 4.3, 40.1, 53.4, 1.5],
|
||||
"薯片": [548, 6.4, 35.2, 51.9, 4.4]
|
||||
};
|
||||
|
||||
/* ============ 二、每日目标(原算法) ============ */
|
||||
const P_RATIO = 0.167, F_RATIO = 0.275, C_RATIO = 0.558;
|
||||
let targetKcalNow = 1800;
|
||||
let targetMacro = { p: 0, f: 0, c: 0 };
|
||||
|
||||
function fmt(n, d) { return Number(n).toFixed(d === undefined ? 0 : d); }
|
||||
|
||||
@@ -185,10 +334,8 @@ function calc() {
|
||||
const steps = +document.getElementById('steps').value || 0;
|
||||
const goal = +document.getElementById('goal').value;
|
||||
|
||||
// 1. BMI
|
||||
const bmi = w / Math.pow(h / 100, 2);
|
||||
|
||||
// 2. Mifflin-St Jeor BMR
|
||||
let bmr;
|
||||
if (sex === 'male') {
|
||||
bmr = 10 * w + 6.25 * h - 5 * age + 5;
|
||||
@@ -196,42 +343,34 @@ function calc() {
|
||||
bmr = 10 * w + 6.25 * h - 5 * age - 161;
|
||||
}
|
||||
|
||||
// 3. 步行能耗:MET 3.5,时间 = 步数 / 6000 小时(100 步/分)
|
||||
const walkHours = steps / 6000;
|
||||
const walkKcal = 3.5 * w * walkHours;
|
||||
const walkHours = steps / 6000; // 100 步/分 → 步数/6000 小时
|
||||
const walkKcal = 3.5 * w * walkHours; // MET 3.5
|
||||
const tdee = bmr * 1.2 + walkKcal;
|
||||
const target = tdee + goal;
|
||||
|
||||
// 4. TDEE = BMR × 1.2(久坐基底)+ 步行能耗
|
||||
const tdee = bmr * 1.2 + walkKcal;
|
||||
targetKcalNow = target;
|
||||
targetMacro = {
|
||||
p: target * P_RATIO / 4,
|
||||
f: target * F_RATIO / 9,
|
||||
c: target * C_RATIO / 4
|
||||
};
|
||||
|
||||
// 5. 目标热量
|
||||
const target = tdee + goal;
|
||||
|
||||
// 6. 三大产能营养素
|
||||
const pG = target * P_RATIO / 4;
|
||||
const fG = target * F_RATIO / 9;
|
||||
const cG = target * C_RATIO / 4;
|
||||
|
||||
document.getElementById('bmi').textContent = fmt(bmi, 1);
|
||||
document.getElementById('bmr').textContent = fmt(bmr, 0);
|
||||
document.getElementById('walk').textContent = fmt(walkKcal, 0);
|
||||
document.getElementById('tdee').textContent = fmt(tdee, 0);
|
||||
document.getElementById('targetKcal').textContent = fmt(target, 0);
|
||||
document.getElementById('bmi').textContent = fmt(bmi, 1);
|
||||
document.getElementById('bmr').textContent = fmt(bmr, 0);
|
||||
document.getElementById('walk').textContent = fmt(walkKcal, 0);
|
||||
document.getElementById('tdee').textContent = fmt(tdee, 0);
|
||||
document.getElementById('targetKcal').textContent = fmt(target, 0);
|
||||
document.getElementById('targetKcal2').textContent = fmt(target, 0);
|
||||
|
||||
document.getElementById('barP').style.width = (P_RATIO * 100) + '%';
|
||||
document.getElementById('barF').style.width = (F_RATIO * 100) + '%';
|
||||
document.getElementById('barC').style.width = (C_RATIO * 100) + '%';
|
||||
|
||||
document.getElementById('macroBody').innerHTML = [
|
||||
['蛋白质', fmt(pG, 0) + ' g(' + fmt(pG / w, 2) + ' g/kg)', '4 kcal/g', fmt(pG * 4, 0) + ' kcal', (P_RATIO * 100).toFixed(1) + '%'],
|
||||
['脂肪', fmt(fG, 0) + ' g', '9 kcal/g', fmt(fG * 9, 0) + ' kcal', (F_RATIO * 100).toFixed(1) + '%'],
|
||||
['碳水化合物', fmt(cG, 0) + ' g', '4 kcal/g', fmt(cG * 4, 0) + ' kcal', (C_RATIO * 100).toFixed(1) + '%']
|
||||
].map(r => '<tr><td>' + r[0] + '</td><td class="num">' + r[1] + '</td><td class="num">' + r[2] + '</td><td class="num">' + r[3] + '</td><td class="num">' + r[4] + '</td></tr>').join('');
|
||||
|
||||
document.getElementById('sevenBody').innerHTML = [
|
||||
['1', '蛋白质', 'tag-yes', '产能', fmt(pG, 0) + ' g', fmt(pG * 4, 0) + ' kcal'],
|
||||
['2', '脂肪', 'tag-yes', '产能', fmt(fG, 0) + ' g', fmt(fG * 9, 0) + ' kcal'],
|
||||
['3', '碳水化合物', 'tag-yes', '产能', fmt(cG, 0) + ' g', fmt(cG * 4, 0) + ' kcal'],
|
||||
['4', '膳食纤维', 'tag-yes', '微弱', '25–30 g', '约 50–60 kcal'],
|
||||
['1', '蛋白质', 'tag-yes', '产能', fmt(targetMacro.p, 0) + ' g', fmt(targetMacro.p * 4, 0) + ' kcal'],
|
||||
['2', '脂肪', 'tag-yes', '产能', fmt(targetMacro.f, 0) + ' g', fmt(targetMacro.f * 9, 0) + ' kcal'],
|
||||
['3', '碳水化合物', 'tag-yes', '产能', fmt(targetMacro.c, 0) + ' g', fmt(targetMacro.c * 4, 0) + ' kcal'],
|
||||
['4', '膳食纤维', 'tag-yes', '微弱', '25–30 g', '约 50–60 kcal'],
|
||||
['5', '维生素', 'tag-no', '不产能', '见下表(13 种)', '0 kcal'],
|
||||
['6', '矿物质', 'tag-no', '不产能', '见下表(14 种)', '0 kcal'],
|
||||
['7', '水', 'tag-no', '不产能', '饮水 1500–1700 mL', '0 kcal']
|
||||
@@ -240,12 +379,198 @@ function calc() {
|
||||
'<td><span class="tag ' + r[2] + '">' + r[3] + '</span></td>' +
|
||||
'<td class="num">' + r[4] + '</td><td class="num">' + r[5] + '</td></tr>'
|
||||
).join('');
|
||||
|
||||
renderMeal();
|
||||
}
|
||||
|
||||
/* ============ 三、今日饮食记录 ============ */
|
||||
let meal = []; // [{name, grams, per:[kcal,p,f,c,fib]}]
|
||||
|
||||
function loadMeal() {
|
||||
try {
|
||||
const raw = localStorage.getItem('mealLog');
|
||||
if (raw) { const arr = JSON.parse(raw); if (Array.isArray(arr)) meal = arr; }
|
||||
} catch (e) { /* 忽略 */ }
|
||||
}
|
||||
function saveMeal() {
|
||||
try { localStorage.setItem('mealLog', JSON.stringify(meal)); } catch (e) { /* 忽略 */ }
|
||||
}
|
||||
|
||||
function findFood(raw) {
|
||||
if (DB[raw]) return raw;
|
||||
const keys = Object.keys(DB);
|
||||
let hit = keys.find(k => k.indexOf(raw) !== -1); // 输入是食物名的一部分
|
||||
if (hit) return hit;
|
||||
hit = keys.find(k => raw.indexOf(k.replace(/(.*?)/g, '')) !== -1); // 输入里含库中食物名
|
||||
return hit || null;
|
||||
}
|
||||
|
||||
function setMsg(text, ok) {
|
||||
const el = document.getElementById('foodMsg');
|
||||
el.textContent = text;
|
||||
el.className = 'msg ' + (ok ? 'msg-ok' : 'msg-err');
|
||||
}
|
||||
|
||||
function addFood() {
|
||||
const raw = document.getElementById('foodInput').value.trim();
|
||||
const grams = +document.getElementById('foodGrams').value || 0;
|
||||
if (!raw) { setMsg('请先输入食物名称。', false); return; }
|
||||
if (grams <= 0) { setMsg('请输入大于 0 的分量(g)。', false); return; }
|
||||
const key = findFood(raw);
|
||||
if (!key) { setMsg('食物库暂未收录「' + raw + '」,请展开下方「自定义添加」。', false); return; }
|
||||
meal.push({ name: key, grams: grams, per: DB[key].slice() });
|
||||
document.getElementById('foodInput').value = '';
|
||||
document.getElementById('foodInput').focus();
|
||||
setMsg('已添加:' + key + ' ' + grams + ' g,约 ' + fmt(DB[key][0] * grams / 100, 0) + ' kcal。', true);
|
||||
saveMeal(); renderMeal();
|
||||
}
|
||||
|
||||
function addCustom() {
|
||||
const name = document.getElementById('cName').value.trim();
|
||||
const grams = +document.getElementById('cGrams').value || 0;
|
||||
if (!name) { setMsg('请填写自定义食物名称。', false); return; }
|
||||
if (grams <= 0) { setMsg('请输入大于 0 的分量(g)。', false); return; }
|
||||
const per = [
|
||||
+document.getElementById('cKcal').value || 0,
|
||||
+document.getElementById('cP').value || 0,
|
||||
+document.getElementById('cF').value || 0,
|
||||
+document.getElementById('cC').value || 0,
|
||||
+document.getElementById('cFib').value || 0
|
||||
];
|
||||
if (per[0] <= 0) { setMsg('请填写该食物的每 100 g 热量。', false); return; }
|
||||
DB[name] = per.slice();
|
||||
rebuildDatalist();
|
||||
meal.push({ name: name, grams: grams, per: per });
|
||||
document.getElementById('cName').value = '';
|
||||
setMsg('已加入自定义食物:' + name + ' ' + grams + ' g。', true);
|
||||
saveMeal(); renderMeal();
|
||||
}
|
||||
|
||||
function removeEntry(i) {
|
||||
meal.splice(i, 1);
|
||||
saveMeal(); renderMeal();
|
||||
}
|
||||
|
||||
function getTotals() {
|
||||
const t = { kcal: 0, p: 0, f: 0, c: 0, fib: 0 };
|
||||
meal.forEach(it => {
|
||||
const k = it.grams / 100;
|
||||
t.kcal += it.per[0] * k;
|
||||
t.p += it.per[1] * k;
|
||||
t.f += it.per[2] * k;
|
||||
t.c += it.per[3] * k;
|
||||
t.fib += it.per[4] * k;
|
||||
});
|
||||
return t;
|
||||
}
|
||||
|
||||
function renderMeal() {
|
||||
const body = document.getElementById('mealBody');
|
||||
const foot = document.getElementById('mealFoot');
|
||||
|
||||
if (!meal.length) {
|
||||
body.innerHTML = '<tr><td colspan="8" class="empty">还没有记录 —— 在上面输入今天吃的东西,就能看到热量。</td></tr>';
|
||||
foot.innerHTML = '';
|
||||
} else {
|
||||
body.innerHTML = meal.map((it, i) => {
|
||||
const k = it.grams / 100;
|
||||
return '<tr>' +
|
||||
'<td>' + it.name + '</td>' +
|
||||
'<td class="num">' + fmt(it.grams, 0) + '</td>' +
|
||||
'<td class="num">' + fmt(it.per[0] * k, 0) + '</td>' +
|
||||
'<td class="num">' + fmt(it.per[1] * k, 1) + '</td>' +
|
||||
'<td class="num">' + fmt(it.per[2] * k, 1) + '</td>' +
|
||||
'<td class="num">' + fmt(it.per[3] * k, 1) + '</td>' +
|
||||
'<td class="num">' + fmt(it.per[4] * k, 1) + '</td>' +
|
||||
'<td><button class="btn-line" data-del="' + i + '">删除</button></td>' +
|
||||
'</tr>';
|
||||
}).join('');
|
||||
|
||||
const t = getTotals();
|
||||
foot.innerHTML = '<tr>' +
|
||||
'<td>合计</td>' +
|
||||
'<td class="num">—</td>' +
|
||||
'<td class="num">' + fmt(t.kcal, 0) + ' kcal</td>' +
|
||||
'<td class="num">' + fmt(t.p, 1) + ' g</td>' +
|
||||
'<td class="num">' + fmt(t.f, 1) + ' g</td>' +
|
||||
'<td class="num">' + fmt(t.c, 1) + ' g</td>' +
|
||||
'<td class="num">' + fmt(t.fib, 1) + ' g</td>' +
|
||||
'<td></td></tr>';
|
||||
}
|
||||
|
||||
const t = getTotals();
|
||||
const pct = targetKcalNow > 0 ? t.kcal / targetKcalNow * 100 : 0;
|
||||
const fill = document.getElementById('progFill');
|
||||
fill.style.width = Math.min(100, pct) + '%';
|
||||
fill.className = pct > 100 ? 'over' : '';
|
||||
|
||||
document.getElementById('intakeKcal').textContent = fmt(t.kcal, 0);
|
||||
document.getElementById('intakeChip').textContent = fmt(t.kcal, 0);
|
||||
|
||||
const remain = targetKcalNow - t.kcal;
|
||||
const lab = document.getElementById('remainLabel');
|
||||
lab.textContent = remain >= 0
|
||||
? '剩余 ' + fmt(remain, 0) + ' kcal(' + fmt(pct, 0) + '%)'
|
||||
: '已超出 ' + fmt(-remain, 0) + ' kcal(' + fmt(pct, 0) + '%)';
|
||||
lab.className = remain >= 0 ? '' : 'over-text';
|
||||
|
||||
renderMacroTable(t);
|
||||
}
|
||||
|
||||
function renderMacroTable(t) {
|
||||
const rows = [
|
||||
['蛋白质', targetMacro.p, t.p, '4 kcal/g', targetMacro.p * 4],
|
||||
['脂肪', targetMacro.f, t.f, '9 kcal/g', targetMacro.f * 9],
|
||||
['碳水化合物', targetMacro.c, t.c, '4 kcal/g', targetMacro.c * 4]
|
||||
];
|
||||
document.getElementById('macroBody').innerHTML = rows.map(r => {
|
||||
const left = r[1] - r[2];
|
||||
return '<tr><td>' + r[0] + '</td>' +
|
||||
'<td class="num">' + fmt(r[1], 0) + ' g</td>' +
|
||||
'<td class="num">' + r[3] + '</td>' +
|
||||
'<td class="num">' + fmt(r[4], 0) + ' kcal</td>' +
|
||||
'<td class="num add-intake">' + fmt(r[2], 1) + ' g</td>' +
|
||||
'<td class="num ' + (left >= 0 ? '' : 'over-text') + '">' +
|
||||
(left >= 0 ? fmt(left, 1) + ' g' : '超 ' + fmt(-left, 1) + ' g') +
|
||||
'</td></tr>';
|
||||
}).join('') +
|
||||
'<tr><td>膳食纤维</td><td class="num">25–30 g</td><td class="num">2 kcal/g</td>' +
|
||||
'<td class="num">50–60 kcal</td><td class="num add-intake">' + fmt(t.fib, 1) + ' g</td>' +
|
||||
'<td class="num">' + (t.fib < 25 ? '还差 ' + fmt(25 - t.fib, 1) + ' g' : '已达标') + '</td></tr>';
|
||||
}
|
||||
|
||||
function rebuildDatalist() {
|
||||
document.getElementById('foodList').innerHTML =
|
||||
Object.keys(DB).map(k => '<option value="' + k + '"></option>').join('');
|
||||
}
|
||||
|
||||
/* ============ 四、事件绑定 ============ */
|
||||
document.getElementById('addBtn').addEventListener('click', addFood);
|
||||
document.getElementById('addCustomBtn').addEventListener('click', addCustom);
|
||||
document.getElementById('foodInput').addEventListener('keydown', e => {
|
||||
if (e.key === 'Enter') addFood();
|
||||
});
|
||||
document.getElementById('foodGrams').addEventListener('keydown', e => {
|
||||
if (e.key === 'Enter') addFood();
|
||||
});
|
||||
document.getElementById('mealBody').addEventListener('click', e => {
|
||||
const idx = e.target.getAttribute('data-del');
|
||||
if (idx !== null) removeEntry(+idx);
|
||||
});
|
||||
document.getElementById('clearBtn').addEventListener('click', () => {
|
||||
if (!meal.length || confirm('确定清空今天的所有饮食记录?')) {
|
||||
meal = []; saveMeal(); renderMeal(); setMsg('已清空今日记录。', true);
|
||||
}
|
||||
});
|
||||
document.querySelectorAll('input, select').forEach(el => {
|
||||
if (el.closest('.food-input') || el.closest('details')) return;
|
||||
el.addEventListener('input', calc);
|
||||
el.addEventListener('change', calc);
|
||||
});
|
||||
|
||||
/* ============ 五、启动 ============ */
|
||||
rebuildDatalist();
|
||||
loadMeal();
|
||||
calc();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user