Files
bmi-calculator/nutrition-calculator.html
T
class03m15 aed0fe4d26 在原热量计算器基础上增加今日饮食记录功能
- 在原有身体参数与每日目标计算之外,改为按今日所吃食物输入,输出摄入热量
- 收录常见食物每 100 g 的热量与蛋白质、脂肪、碳水化合物、膳食纤维含量
- 以克数换算每条记录,实时汇总今日摄入热量与四大营养素
- 将摄入汇总与每日目标热量、三大产能营养素目标对比,显示剩余或超出
- 支持未收录食物的自定义每 100 g 数值录入,记录保存在本机浏览器
2026-10-08 02:32:45 +00:00

578 lines
26 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>每日热量与七大营养素计算器</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0; padding: 24px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
background: #f4f6f8; color: #1f2933; line-height: 1.6;
}
.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 {
background: #fff; border-radius: 12px; padding: 20px;
box-shadow: 0 1px 3px rgba(0,0,0,.08); margin-bottom: 18px;
}
.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], input[type=text], select {
width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 8px;
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; }
.radios label { display: flex; align-items: center; gap: 6px; font-size: 14px; color: #1f2933; margin: 0; cursor: pointer; }
.hero {
background: linear-gradient(135deg, #0f766e, #14b8a6); color: #fff;
border-radius: 12px; padding: 22px; margin-bottom: 18px;
}
.hero .big { font-size: 40px; font-weight: 700; line-height: 1.1; }
.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 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; }
th { font-size: 12px; color: #6b7280; font-weight: 600; background: #fafbfc; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.tag { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 99px; }
.tag-yes { background: #d1fae5; color: #065f46; }
.tag-no { background: #e5e7eb; color: #4b5563; }
.macro-bar { height: 10px; border-radius: 99px; overflow: hidden; display: flex; margin: 6px 0 16px; }
.macro-bar span { height: 100%; }
.legend { display: flex; gap: 18px; font-size: 12px; color: #4b5563; flex-wrap: wrap; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; }
.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="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>
<div class="grid">
<div>
<label>性别</label>
<div class="radios">
<label><input type="radio" name="sex" value="female" checked>女</label>
<label><input type="radio" name="sex" value="male">男</label>
</div>
</div>
<div><label>年龄(岁)</label><input type="number" id="age" value="30" min="10" max="90"></div>
<div><label>身高(cm)</label><input type="number" id="height" value="165" min="120" max="220"></div>
<div><label>体重(kg)</label><input type="number" id="weight" value="51" min="30" max="200" step="0.1"></div>
<div><label>每日步数</label><input type="number" id="steps" value="10000" min="0" max="50000" step="500"></div>
<div>
<label>目标</label>
<select id="goal">
<option value="0" selected>维持体重</option>
<option value="200">小幅增重(+200 kcal)</option>
<option value="300">增重(+300 kcal)</option>
<option value="-300">减脂(−300 kcal)</option>
</select>
</div>
</div>
</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>
<div class="macro-bar">
<span id="barP" style="background:#0d9488"></span>
<span id="barF" style="background:#f59e0b"></span>
<span id="barC" style="background:#3b82f6"></span>
</div>
<div class="legend" style="margin-bottom:14px">
<span><i class="dot" style="background:#0d9488"></i>蛋白质 16.7%</span>
<span><i class="dot" style="background:#f59e0b"></i>脂肪 27.5%</span>
<span><i class="dot" style="background:#3b82f6"></i>碳水化合物 55.8%</span>
</div>
<table>
<thead>
<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>
<table>
<thead>
<tr><th>#</th><th>营养素</th><th>是否产能</th><th class="num">每日推荐量</th><th class="num">能量</th></tr>
</thead>
<tbody id="sevenBody"></tbody>
</table>
<div class="note">
蛋白质/脂肪/碳水为产能营养素;膳食纤维 25–30 g 建议量,能量按 2 kcal/g 计(通常并入碳水账目);<br>
维生素、矿物质、水不产能,其参考摄入量见下。
</div>
</div>
<div class="card">
<h2>⑤ 微量营养素参考摄入量</h2>
<div class="cols">
<table>
<thead><tr><th>维生素</th><th class="num">每日量</th></tr></thead>
<tbody>
<tr><td>维生素 A</td><td class="num">660 μg RAE</td></tr>
<tr><td>维生素 D</td><td class="num">10 μg</td></tr>
<tr><td>维生素 E</td><td class="num">14 mg α-TE</td></tr>
<tr><td>维生素 K</td><td class="num">80 μg</td></tr>
<tr><td>维生素 B1</td><td class="num">1.2 mg</td></tr>
<tr><td>维生素 B2</td><td class="num">1.2 mg</td></tr>
<tr><td>烟酸 (B3)</td><td class="num">12 mg NE</td></tr>
<tr><td>维生素 B6</td><td class="num">1.4 mg</td></tr>
<tr><td>叶酸 (B9)</td><td class="num">400 μg DFE</td></tr>
<tr><td>维生素 B12</td><td class="num">2.4 μg</td></tr>
<tr><td>维生素 C</td><td class="num">100 mg</td></tr>
</tbody>
</table>
<table>
<thead><tr><th>矿物质 / 水</th><th class="num">每日量</th></tr></thead>
<tbody>
<tr><td>钙</td><td class="num">800 mg</td></tr>
<tr><td>磷</td><td class="num">720 mg</td></tr>
<tr><td>钾</td><td class="num">2000 mg</td></tr>
<tr><td>钠</td><td class="num">1500 mg(盐&lt;5 g)</td></tr>
<tr><td>镁</td><td class="num">330 mg</td></tr>
<tr><td>铁</td><td class="num">18 mg</td></tr>
<tr><td>锌</td><td class="num">8.5 mg</td></tr>
<tr><td>碘</td><td class="num">120 μg</td></tr>
<tr><td>硒</td><td class="num">60 μg</td></tr>
<tr><td>铜 / 锰</td><td class="num">0.8 mg / 4.5 mg</td></tr>
<tr><td>水(饮水)</td><td class="num">1500–1700 mL</td></tr>
</tbody>
</table>
</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); }
function calc() {
const sex = document.querySelector('input[name=sex]:checked').value;
const age = +document.getElementById('age').value || 30;
const h = +document.getElementById('height').value || 165;
const w = +document.getElementById('weight').value || 51;
const steps = +document.getElementById('steps').value || 0;
const goal = +document.getElementById('goal').value;
const bmi = w / Math.pow(h / 100, 2);
let bmr;
if (sex === 'male') {
bmr = 10 * w + 6.25 * h - 5 * age + 5;
} else {
bmr = 10 * w + 6.25 * h - 5 * age - 161;
}
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;
targetKcalNow = target;
targetMacro = {
p: target * P_RATIO / 4,
f: target * F_RATIO / 9,
c: 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('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('sevenBody').innerHTML = [
['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']
].map(r =>
'<tr><td>' + r[0] + '</td><td>' + r[1] + '</td>' +
'<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>
</html>