我新增了每日摄入热量与BMI计算器页面,用于录入当天食物后自动算出总热量、BMI 和每日所需热量
This commit is contained in:
+269
@@ -0,0 +1,269 @@
|
||||
<!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; }
|
||||
body {
|
||||
font-family: -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||
margin: 0; padding: 24px;
|
||||
background: linear-gradient(135deg, #eef2ff 0%, #f8fafc 100%);
|
||||
color: #1e293b;
|
||||
}
|
||||
.wrap { max-width: 960px; margin: 0 auto; }
|
||||
h1 { font-size: 22px; margin: 0 0 4px; }
|
||||
.sub { color: #64748b; font-size: 13px; margin-bottom: 20px; }
|
||||
.card {
|
||||
background: #fff; border-radius: 14px; padding: 20px;
|
||||
box-shadow: 0 4px 20px rgba(15,23,42,.07);
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.card h2 { font-size: 15px; margin: 0 0 14px; color: #334155; display: flex; align-items: center; gap: 8px; }
|
||||
.card h2::before { content: ""; width: 4px; height: 16px; background: #6366f1; border-radius: 2px; }
|
||||
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
|
||||
label { display: block; font-size: 12px; color: #64748b; margin-bottom: 5px; }
|
||||
input, select {
|
||||
width: 100%; padding: 9px 10px; border: 1px solid #e2e8f0; border-radius: 8px;
|
||||
font-size: 14px; background: #f8fafc; transition: .15s;
|
||||
}
|
||||
input:focus, select:focus { outline: none; border-color: #6366f1; background: #fff; box-shadow: 0 0 0 3px rgba(99,102,241,.12); }
|
||||
button {
|
||||
border: none; border-radius: 8px; padding: 10px 16px; font-size: 14px;
|
||||
cursor: pointer; font-weight: 500; transition: .15s;
|
||||
}
|
||||
.btn-primary { background: #6366f1; color: #fff; }
|
||||
.btn-primary:hover { background: #4f46e5; }
|
||||
.btn-ghost { background: #f1f5f9; color: #475569; }
|
||||
.btn-ghost:hover { background: #e2e8f0; }
|
||||
.btn-del { background: #fee2e2; color: #dc2626; padding: 6px 10px; font-size: 12px; }
|
||||
.btn-del:hover { background: #fecaca; }
|
||||
.food-row { display: grid; grid-template-columns: 2fr 1fr 1fr auto; gap: 8px; margin-bottom: 8px; align-items: end; }
|
||||
table { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 6px; }
|
||||
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #f1f5f9; }
|
||||
th { font-size: 12px; color: #64748b; font-weight: 600; background: #f8fafc; }
|
||||
th:first-child { border-radius: 8px 0 0 0; }
|
||||
th:last-child { border-radius: 0 8px 0 0; }
|
||||
tbody tr:hover { background: #fafbff; }
|
||||
.num { text-align: right; font-variant-numeric: tabular-nums; }
|
||||
.total-row td { font-weight: 700; border-top: 2px solid #e2e8f0; background: #f8fafc; }
|
||||
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; }
|
||||
.stat { background: #f8fafc; border-radius: 10px; padding: 14px; text-align: center; }
|
||||
.stat .v { font-size: 26px; font-weight: 700; line-height: 1.1; }
|
||||
.stat .k { font-size: 12px; color: #64748b; margin-top: 4px; }
|
||||
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; margin-top: 8px; }
|
||||
.b-under { background: #dbeafe; color: #1d4ed8; }
|
||||
.b-normal { background: #dcfce7; color: #15803d; }
|
||||
.b-over { background: #fef3c7; color: #b45309; }
|
||||
.b-obese { background: #fee2e2; color: #b91c1c; }
|
||||
.bar { height: 8px; background: #e2e8f0; border-radius: 999px; overflow: hidden; margin-top: 10px; }
|
||||
.bar > span { display: block; height: 100%; background: linear-gradient(90deg, #6366f1, #22d3ee); border-radius: 999px; transition: width .4s; }
|
||||
.hint { font-size: 12px; color: #94a3b8; margin-top: 8px; }
|
||||
.empty { text-align: center; color: #94a3b8; padding: 20px; font-size: 13px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<h1>🍽️ 每日摄入热量 & BMI 计算器</h1>
|
||||
<div class="sub">输入身体数据和当天吃的食物,自动计算 BMI、所需热量与摄入总热量。</div>
|
||||
|
||||
<!-- 身体信息 -->
|
||||
<div class="card">
|
||||
<h2>1. 身体信息</h2>
|
||||
<div class="grid">
|
||||
<div><label>身高 (cm)</label><input type="number" id="height" value="170" min="50" max="250"></div>
|
||||
<div><label>体重 (kg)</label><input type="number" id="weight" value="65" min="10" max="300" step="0.1"></div>
|
||||
<div><label>年龄</label><input type="number" id="age" value="25" min="1" max="120"></div>
|
||||
<div><label>性别</label>
|
||||
<select id="gender"><option value="male">男</option><option value="female">女</option></select>
|
||||
</div>
|
||||
<div><label>活动量</label>
|
||||
<select id="activity">
|
||||
<option value="1.2">久坐少动</option>
|
||||
<option value="1.375">轻度活动</option>
|
||||
<option value="1.55" selected>中度活动</option>
|
||||
<option value="1.725">高强度活动</option>
|
||||
<option value="1.9">专业运动员</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 食物录入 -->
|
||||
<div class="card">
|
||||
<h2>2. 今日食物摄入</h2>
|
||||
<div class="food-row" style="font-size:12px;color:#64748b;margin-bottom:4px;">
|
||||
<span>食物名称</span><span>数量</span><span>热量 (kcal)</span><span></span>
|
||||
</div>
|
||||
<div id="foodList"></div>
|
||||
<div style="display:flex;gap:10px;margin-top:12px;">
|
||||
<button class="btn-primary" onclick="addFood()">+ 添加食物</button>
|
||||
<button class="btn-ghost" onclick="addSample()">填入示例</button>
|
||||
<button class="btn-ghost" onclick="clearFoods()">清空</button>
|
||||
</div>
|
||||
<div class="hint">💡 热量可自己填,也可参考常见食物:米饭(1碗≈230)、鸡蛋(1个≈70)、苹果(1个≈95)、牛奶(1杯≈150)、鸡胸肉(100g≈165)。</div>
|
||||
</div>
|
||||
|
||||
<!-- 结果表格 -->
|
||||
<div class="card">
|
||||
<h2>3. 内容表格</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>食物名称</th><th class="num">数量</th><th class="num">热量 (kcal)</th><th class="num">占比</th></tr>
|
||||
</thead>
|
||||
<tbody id="resultBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- 数据汇总 -->
|
||||
<div class="card">
|
||||
<h2>4. 数据汇总</h2>
|
||||
<div class="stats">
|
||||
<div class="stat"><div class="v" id="bmiVal">--</div><div class="k">BMI</div><div id="bmiBadge"></div></div>
|
||||
<div class="stat"><div class="v" id="tdeeVal">--</div><div class="k">每日所需热量 (kcal)</div></div>
|
||||
<div class="stat"><div class="v" id="intakeVal">0</div><div class="k">今日摄入热量 (kcal)</div></div>
|
||||
<div class="stat"><div class="v" id="diffVal">--</div><div class="k">热量盈亏 (kcal)</div></div>
|
||||
</div>
|
||||
<div class="bar"><span id="progressBar" style="width:0%"></span></div>
|
||||
<div class="hint" id="summaryHint">摄入量 / 所需量 的完成度会显示在这里。</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let foods = [];
|
||||
|
||||
function addFood(name = "", qty = "", cal = "") {
|
||||
foods.push({ name, qty, cal: cal === "" ? "" : Number(cal) });
|
||||
renderFoodInputs();
|
||||
calculate();
|
||||
}
|
||||
function addSample() {
|
||||
foods = [
|
||||
{ name: "米饭", qty: "1 碗", cal: 230 },
|
||||
{ name: "鸡蛋", qty: "2 个", cal: 140 },
|
||||
{ name: "苹果", qty: "1 个", cal: 95 },
|
||||
{ name: "牛奶", qty: "1 杯", cal: 150 },
|
||||
{ name: "鸡胸肉", qty: "100 g", cal: 165 }
|
||||
];
|
||||
renderFoodInputs();
|
||||
calculate();
|
||||
}
|
||||
function clearFoods() {
|
||||
foods = [];
|
||||
renderFoodInputs();
|
||||
calculate();
|
||||
}
|
||||
function updateFood(i, field, val) {
|
||||
foods[i][field] = field === "cal" ? (val === "" ? "" : Number(val)) : val;
|
||||
calculate();
|
||||
}
|
||||
function removeFood(i) {
|
||||
foods.splice(i, 1);
|
||||
renderFoodInputs();
|
||||
calculate();
|
||||
}
|
||||
|
||||
function renderFoodInputs() {
|
||||
const box = document.getElementById("foodList");
|
||||
if (foods.length === 0) {
|
||||
box.innerHTML = '<div class="empty">还没有添加食物,点击下方按钮开始 👇</div>';
|
||||
return;
|
||||
}
|
||||
box.innerHTML = foods.map((f, i) => `
|
||||
<div class="food-row">
|
||||
<input placeholder="食物名称" value="${f.name}" oninput="updateFood(${i},'name',this.value)">
|
||||
<input placeholder="如 1碗" value="${f.qty}" oninput="updateFood(${i},'qty',this.value)">
|
||||
<input type="number" placeholder="kcal" value="${f.cal}" oninput="updateFood(${i},'cal',this.value)">
|
||||
<button class="btn-del" onclick="removeFood(${i})">删除</button>
|
||||
</div>`).join("");
|
||||
}
|
||||
|
||||
function calcBMI() {
|
||||
const h = parseFloat(document.getElementById("height").value) / 100;
|
||||
const w = parseFloat(document.getElementById("weight").value);
|
||||
if (!h || !w) return null;
|
||||
return w / (h * h);
|
||||
}
|
||||
|
||||
function calcTDEE() {
|
||||
const h = parseFloat(document.getElementById("height").value);
|
||||
const w = parseFloat(document.getElementById("weight").value);
|
||||
const age = parseFloat(document.getElementById("age").value);
|
||||
const gender = document.getElementById("gender").value;
|
||||
const act = parseFloat(document.getElementById("activity").value);
|
||||
if (!h || !w || !age) return null;
|
||||
// Mifflin-St Jeor 公式
|
||||
let bmr = 10 * w + 6.25 * h - 5 * age + (gender === "male" ? 5 : -161);
|
||||
return bmr * act;
|
||||
}
|
||||
|
||||
function calculate() {
|
||||
const total = foods.reduce((s, f) => s + (Number(f.cal) || 0), 0);
|
||||
|
||||
// 表格
|
||||
const body = document.getElementById("resultBody");
|
||||
if (foods.length === 0) {
|
||||
body.innerHTML = '<tr><td colspan="4" class="empty">暂无数据</td></tr>';
|
||||
} else {
|
||||
body.innerHTML = foods.map(f => {
|
||||
const c = Number(f.cal) || 0;
|
||||
const pct = total > 0 ? (c / total * 100).toFixed(1) : "0.0";
|
||||
return `<tr><td>${f.name || "—"}</td><td class="num">${f.qty || "—"}</td><td class="num">${c}</td><td class="num">${pct}%</td></tr>`;
|
||||
}).join("") +
|
||||
`<tr class="total-row"><td>合计</td><td class="num">—</td><td class="num">${total}</td><td class="num">100%</td></tr>`;
|
||||
}
|
||||
|
||||
// BMI
|
||||
const bmi = calcBMI();
|
||||
const bmiVal = document.getElementById("bmiVal");
|
||||
const badge = document.getElementById("bmiBadge");
|
||||
if (bmi) {
|
||||
bmiVal.textContent = bmi.toFixed(1);
|
||||
let cls, txt;
|
||||
if (bmi < 18.5) { cls = "b-under"; txt = "偏瘦"; }
|
||||
else if (bmi < 24) { cls = "b-normal"; txt = "正常"; }
|
||||
else if (bmi < 28) { cls = "b-over"; txt = "超重"; }
|
||||
else { cls = "b-obese"; txt = "肥胖"; }
|
||||
badge.className = "badge " + cls;
|
||||
badge.textContent = txt;
|
||||
} else {
|
||||
bmiVal.textContent = "--"; badge.className = ""; badge.textContent = "";
|
||||
}
|
||||
|
||||
// TDEE
|
||||
const tdee = calcTDEE();
|
||||
document.getElementById("tdeeVal").textContent = tdee ? Math.round(tdee) : "--";
|
||||
|
||||
// 摄入
|
||||
document.getElementById("intakeVal").textContent = total;
|
||||
|
||||
// 盈亏
|
||||
const diffEl = document.getElementById("diffVal");
|
||||
if (tdee) {
|
||||
const diff = total - tdee;
|
||||
diffEl.textContent = (diff > 0 ? "+" : "") + Math.round(diff);
|
||||
diffEl.style.color = diff > 100 ? "#dc2626" : diff < -100 ? "#2563eb" : "#15803d";
|
||||
const pct = Math.min(100, (total / tdee) * 100);
|
||||
document.getElementById("progressBar").style.width = pct + "%";
|
||||
const tip = diff > 100 ? "今日摄入偏高,注意控制 🍰"
|
||||
: diff < -100 ? "今日摄入偏低,注意补充能量 🥗"
|
||||
: "今日摄入比较均衡,继续保持 👍";
|
||||
document.getElementById("summaryHint").textContent = `完成度 ${pct.toFixed(0)}% · ${tip}`;
|
||||
} else {
|
||||
diffEl.textContent = "--";
|
||||
document.getElementById("progressBar").style.width = "0%";
|
||||
document.getElementById("summaryHint").textContent = "请填写完整的身体信息以计算对比结果。";
|
||||
}
|
||||
}
|
||||
|
||||
// 身体信息变化时重新计算
|
||||
["height","weight","age","gender","activity"].forEach(id => {
|
||||
document.getElementById(id).addEventListener("input", calculate);
|
||||
});
|
||||
|
||||
// 初始化示例
|
||||
addSample();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user