Files
bmi-calculator/nutrient-calculator.html

292 lines
11 KiB
HTML
Raw Permalink 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; margin: 0; padding: 0; }
body {
font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
background: linear-gradient(135deg, #43cea2 0%, #185a9d 100%);
min-height: 100vh;
padding: 24px 16px;
display: flex;
justify-content: center;
}
.wrap { width: 100%; max-width: 560px; }
.card {
background: #fff;
border-radius: 20px;
box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
padding: 30px;
margin-bottom: 20px;
}
h1 { font-size: 23px; color: #1f2937; text-align: center; }
.subtitle {
text-align: center; color: #9ca3af; font-size: 13px; margin: 6px 0 24px;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16px;
}
.full { grid-column: 1 / -1; }
label {
display: block; font-size: 13px; color: #4b5563;
margin-bottom: 7px; font-weight: 600;
}
input, select {
width: 100%; padding: 11px 12px;
border: 2px solid #e5e7eb; border-radius: 10px;
font-size: 15px; outline: none; background: #fff;
transition: border-color 0.2s;
}
input:focus, select:focus { border-color: #43cea2; }
button {
width: 100%; padding: 14px; border: none; border-radius: 10px;
background: linear-gradient(135deg, #43cea2 0%, #185a9d 100%);
color: #fff; font-size: 16px; font-weight: 600;
cursor: pointer; margin-top: 22px;
transition: transform 0.15s, box-shadow 0.15s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 8px 20px rgba(24, 90, 157, 0.4);
}
.error { color: #ef4444; text-align: center; font-size: 14px; margin-top: 14px; }
.result { display: none; }
.result.show { display: block; animation: fade 0.35s; }
@keyframes fade {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.summary {
text-align: center; padding: 18px; border-radius: 14px;
background: #ecfdf5; margin-bottom: 22px;
}
.summary .big { font-size: 38px; font-weight: 800; color: #059669; }
.summary .big span { font-size: 16px; font-weight: 600; }
.summary .desc { font-size: 13px; color: #6b7280; margin-top: 6px; }
h2 {
font-size: 15px; color: #374151; margin: 22px 0 10px;
padding-left: 10px; border-left: 4px solid #43cea2;
}
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 9px 8px; text-align: left; border-bottom: 1px solid #f0f0f0; }
th { color: #9ca3af; font-size: 12px; font-weight: 600; }
td.val { text-align: right; font-weight: 700; color: #185a9d; }
td.unit { text-align: right; color: #9ca3af; font-size: 12px; width: 70px; }
.tip {
margin-top: 22px; padding: 14px; border-radius: 12px;
background: #f9fafb; font-size: 12px; color: #6b7280; line-height: 1.7;
}
</style>
</head>
<body>
<div class="wrap">
<div class="card">
<h1>每日营养素需要量计算器</h1>
<p class="subtitle">根据你的身体数据估算每日各类营养素摄入目标</p>
<div class="grid">
<div>
<label for="gender">性别</label>
<select id="gender">
<option value="female">女</option>
<option value="male">男</option>
</select>
</div>
<div>
<label for="age">年龄(岁)</label>
<input type="number" id="age" placeholder="20" min="1" max="120">
</div>
<div>
<label for="height">身高(cm)</label>
<input type="number" id="height" placeholder="165" step="0.1">
</div>
<div>
<label for="weight">体重(kg)</label>
<input type="number" id="weight" placeholder="55" step="0.1">
</div>
<div>
<label for="activity">活动量</label>
<select id="activity">
<option value="1.2">久坐(几乎不运动)</option>
<option value="1.375" selected>轻度(每周 1~3 次)</option>
<option value="1.55">中等(每周 3~5 次)</option>
<option value="1.725">大量(每周 6~7 次)</option>
</select>
</div>
<div>
<label for="goal">目标</label>
<select id="goal">
<option value="lose">减脂</option>
<option value="maintain" selected>保持</option>
<option value="gain">增肌</option>
</select>
</div>
</div>
<button onclick="calc()">计算每日需要量</button>
<div id="error" class="error"></div>
</div>
<div id="result" class="result">
<div class="card">
<div class="summary">
<div class="big" id="calBig">--<span> 千卡</span></div>
<div class="desc" id="calDesc">每日建议总热量</div>
</div>
<h2>产能营养素</h2>
<table>
<thead>
<tr><th>营养素</th><th style="text-align:right">推荐量</th><th></th></tr>
</thead>
<tbody>
<tr><td>蛋白质</td><td class="val" id="protein">--</td><td class="unit">g</td></tr>
<tr><td>脂肪</td><td class="val" id="fat">--</td><td class="unit">g</td></tr>
<tr><td>碳水化合物</td><td class="val" id="carb">--</td><td class="unit">g</td></tr>
<tr><td>膳食纤维</td><td class="val" id="fiber">--</td><td class="unit">g</td></tr>
<tr><td>水</td><td class="val" id="water">--</td><td class="unit">ml</td></tr>
</tbody>
</table>
<h2>主要维生素</h2>
<table>
<tbody>
<tr><td>维生素 A</td><td class="val" id="vita">--</td><td class="unit">µg</td></tr>
<tr><td>维生素 B1</td><td class="val" id="vitb1">--</td><td class="unit">mg</td></tr>
<tr><td>维生素 B2</td><td class="val" id="vitb2">--</td><td class="unit">mg</td></tr>
<tr><td>维生素 C</td><td class="val" id="vitc">--</td><td class="unit">mg</td></tr>
<tr><td>维生素 D</td><td class="val" id="vitd">--</td><td class="unit">µg</td></tr>
<tr><td>维生素 E</td><td class="val" id="vite">--</td><td class="unit">mg</td></tr>
</tbody>
</table>
<h2>主要矿物质</h2>
<table>
<tbody>
<tr><td>钙</td><td class="val" id="ca">--</td><td class="unit">mg</td></tr>
<tr><td>铁</td><td class="val" id="fe">--</td><td class="unit">mg</td></tr>
<tr><td>锌</td><td class="val" id="zn">--</td><td class="unit">mg</td></tr>
<tr><td>镁</td><td class="val" id="mg">--</td><td class="unit">mg</td></tr>
<tr><td>钾</td><td class="val" id="k">--</td><td class="unit">mg</td></tr>
<tr><td>钠</td><td class="val" id="na">--</td><td class="unit">mg</td></tr>
</tbody>
</table>
<div class="tip">
⚠️ 结果为基于通用公式与膳食参考摄入量(DRI)的<b>估算值</b>,仅供参考,不能替代医生或营养师的专业建议。
疾病、孕期、哺乳期等特殊人群请遵医嘱。
</div>
</div>
</div>
</div>
<script>
// 维生素与矿物质参考摄入量(按性别,中国居民膳食参考摄入量近似值)
function micros(gender) {
if (gender === "male") {
return { va: 800, b1: 1.4, b2: 1.4, vc: 100, vd: 10, ve: 14,
ca: 800, fe: 12, zn: 12.5, mg: 330, k: 2000, na: 1500 };
}
return { va: 700, b1: 1.2, b2: 1.2, vc: 100, vd: 10, ve: 14,
ca: 800, fe: 20, zn: 7.5, mg: 330, k: 2000, na: 1500 };
}
const goalText = {
lose: "(减脂目标:热量减少约 20%)",
maintain: "(保持目标:维持当前体重)",
gain: "(增肌目标:热量增加约 15%)"
};
function calc() {
const gender = document.getElementById("gender").value;
const age = parseFloat(document.getElementById("age").value);
const h = parseFloat(document.getElementById("height").value);
const w = parseFloat(document.getElementById("weight").value);
const factor = parseFloat(document.getElementById("activity").value);
const goal = document.getElementById("goal").value;
const errorEl = document.getElementById("error");
const resultEl = document.getElementById("result");
errorEl.textContent = "";
resultEl.classList.remove("show");
if ([age, h, w].some(v => isNaN(v) || v <= 0)) {
errorEl.textContent = "请完整填写有效的年龄、身高和体重。";
return;
}
if (h < 50 || h > 250) {
errorEl.textContent = "身高请输入 50 ~ 250 厘米之间的数值。";
return;
}
// 1. 基础代谢率 BMR(Mifflin-St Jeor 公式)
let bmr = 10 * w + 6.25 * h - 5 * age;
bmr += gender === "male" ? 5 : -161;
// 2. 每日总消耗 TDEE
let tdee = bmr * factor;
// 3. 按目标调整热量
let calories = tdee;
if (goal === "lose") calories = tdee * 0.8;
if (goal === "gain") calories = tdee * 1.15;
// 4. 三大产能营养素
// 蛋白质:减脂/增肌 1.8 g/kg,保持 1.2 g/kg
let proteinPerKg = goal === "maintain" ? 1.2 : 1.8;
let protein = proteinPerKg * w;
// 脂肪:约占总热量 27%
let fat = (calories * 0.27) / 9;
// 碳水:剩余热量
let carbs = (calories - protein * 4 - fat * 9) / 4;
if (carbs < 0) carbs = 0;
// 5. 膳食纤维(约 14g / 1000 kcal)与水(约 33ml / kg)
let fiber = (calories / 1000) * 14;
let water = Math.round((w * 33) / 100) * 100;
// 6. 维生素与矿物质
const m = micros(gender);
// 输出
const set = (id, val) => document.getElementById(id).textContent = val;
document.getElementById("calBig").innerHTML = Math.round(calories) + "<span> 千卡</span>";
document.getElementById("calDesc").textContent = goalText[goal];
set("protein", Math.round(protein));
set("fat", Math.round(fat));
set("carb", Math.round(carbs));
set("fiber", Math.round(fiber));
set("water", water);
set("vita", m.va);
set("vitb1", m.b1);
set("vitb2", m.b2);
set("vitc", m.vc);
set("vitd", m.vd);
set("vite", m.ve);
set("ca", m.ca);
set("fe", m.fe);
set("zn", m.zn);
set("mg", m.mg);
set("k", m.k);
set("na", m.na);
resultEl.classList.add("show");
resultEl.scrollIntoView({ behavior: "smooth" });
}
// 回车即计算
document.addEventListener("keydown", e => {
if (e.key === "Enter") calc();
});
</script>
</body>
</html>