Files
nutrition-calculator/index.html
T
class07m04 58f273da78 新增每日热量与营养素需求计算器
同时估算每日所需热量与营养素(蛋白质、脂肪、碳水化合物、膳食纤维、饮水、维生素与矿物质)。
2026-09-24 00:30:01 +00:00

489 lines
19 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; margin: 0; padding: 0; }
body {
font-family: -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
background: linear-gradient(135deg, #eef2ff 0%, #f8fafc 100%);
color: #1e293b;
line-height: 1.6;
padding: 24px 16px;
min-height: 100vh;
}
.card {
max-width: 720px;
margin: 0 auto;
background: #fff;
border-radius: 18px;
box-shadow: 0 10px 40px rgba(79, 70, 229, 0.12);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #4f46e5, #7c3aed);
color: #fff;
padding: 24px 28px;
}
.header h1 { font-size: 20px; font-weight: 700; }
.header p { font-size: 13px; opacity: 0.9; margin-top: 4px; }
.body { padding: 24px 28px 28px; }
.row { display: flex; gap: 16px; flex-wrap: wrap; }
.field { flex: 1; min-width: 120px; margin-bottom: 16px; }
label { display: block; font-size: 13px; font-weight: 600; color: #475569; margin-bottom: 6px; }
input, select {
width: 100%;
padding: 10px 12px;
border: 1.5px solid #cbd5e1;
border-radius: 10px;
font-size: 15px;
color: #1e293b;
background: #f8fafc;
transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus {
outline: none;
border-color: #6366f1;
background: #fff;
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}
.seg { display: flex; gap: 10px; }
.seg button {
flex: 1;
padding: 10px;
border: 1.5px solid #cbd5e1;
background: #f8fafc;
border-radius: 10px;
font-size: 14px;
cursor: pointer;
color: #475569;
font-weight: 600;
transition: all .15s;
}
.seg button.active { background: #4f46e5; border-color: #4f46e5; color: #fff; }
.calc-btn {
width: 100%;
padding: 14px;
background: linear-gradient(135deg, #4f46e5, #7c3aed);
color: #fff;
border: none;
border-radius: 12px;
font-size: 16px;
font-weight: 700;
cursor: pointer;
margin-top: 8px;
transition: transform .1s, box-shadow .15s;
}
.calc-btn:hover { box-shadow: 0 8px 20px rgba(79,70,229,0.35); }
.calc-btn:active { transform: scale(0.98); }
#results { margin-top: 26px; display: none; }
.bmi-badge { border-radius: 12px; padding: 16px; margin-bottom: 18px; color: #fff; }
.bmi-badge .big { font-size: 30px; font-weight: 800; line-height: 1.1; }
.bmi-badge .label { font-size: 13px; opacity: .95; }
.bmi-badge .status { font-size: 15px; font-weight: 700; margin-top: 6px; }
h2.sec {
font-size: 15px;
font-weight: 800;
color: #312e81;
margin: 24px 0 12px;
padding-left: 10px;
border-left: 4px solid #6366f1;
}
.result-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat {
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 14px;
}
.stat .k { font-size: 12px; color: #64748b; font-weight: 600; }
.stat .v { font-size: 21px; font-weight: 800; color: #4f46e5; margin-top: 2px; }
.stat .v small { font-size: 12px; font-weight: 600; color: #94a3b8; }
.target {
margin-top: 14px;
background: linear-gradient(135deg, #ecfdf5, #d1fae5);
border: 1.5px solid #6ee7b7;
border-radius: 12px;
padding: 16px;
}
.target .t-title { font-size: 13px; font-weight: 700; color: #047857; }
.target .t-value { font-size: 25px; font-weight: 800; color: #059669; margin: 4px 0; }
.target .t-desc { font-size: 13px; color: #065f46; }
.macro { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.macro .m-card {
border-radius: 12px;
padding: 14px;
border: 1px solid #e2e8f0;
background: #fff;
}
.macro .m-name { font-size: 13px; font-weight: 700; color: #475569; }
.macro .m-val { font-size: 22px; font-weight: 800; margin: 4px 0; }
.macro .m-unit { font-size: 12px; font-weight: 600; color: #94a3b8; }
.macro .m-sub { font-size: 12px; color: #64748b; }
.protein .m-val { color: #dc2626; }
.fat .m-val { color: #d97706; }
.carb .m-val { color: #2563eb; }
table.nut {
width: 100%;
border-collapse: collapse;
font-size: 13.5px;
margin-top: 4px;
}
table.nut th, table.nut td {
padding: 9px 10px;
text-align: left;
border-bottom: 1px solid #eef2f7;
}
table.nut th {
background: #f1f5f9;
color: #475569;
font-size: 12.5px;
font-weight: 700;
position: sticky;
top: 0;
}
table.nut td.num { font-weight: 800; color: #4f46e5; text-align: right; }
table.nut td.unit { color: #64748b; }
table.nut tr:hover td { background: #f8fafc; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.note {
margin-top: 16px;
font-size: 12.5px;
color: #64748b;
background: #f1f5f9;
border-left: 4px solid #cbd5e1;
padding: 12px 14px;
border-radius: 8px;
}
.note.warn { background: #fef2f2; border-left-color: #f87171; color: #991b1b; }
.pill {
display: inline-block;
font-size: 12px;
background: #eef2ff;
color: #4338ca;
border-radius: 999px;
padding: 3px 10px;
font-weight: 700;
margin-bottom: 8px;
}
@media (max-width: 520px) {
.result-grid, .macro, .two-col { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<div class="card">
<div class="header">
<h1>每日热量与营养素需求计算器</h1>
<p>计算热量目标(BMR / TDEE),并推导蛋白质、脂肪、碳水、纤维、饮水与维生素矿物质需求</p>
</div>
<div class="body">
<div class="field">
<label>性别</label>
<div class="seg" id="genderSeg">
<button type="button" data-gender="male" class="active">男</button>
<button type="button" data-gender="female">女</button>
</div>
</div>
<div class="row">
<div class="field">
<label>年龄(岁)</label>
<input type="number" id="age" value="15" min="6" max="100">
</div>
<div class="field">
<label>身高(cm)</label>
<input type="number" id="height" value="172" min="100" max="230">
</div>
<div class="field">
<label>体重(kg)</label>
<input type="number" id="weight" value="42.5" step="0.1" min="20" max="250">
</div>
</div>
<div class="field">
<label>日常活动量</label>
<select id="activity">
<option value="1.2">久坐(几乎不运动,&lt;5000 步)</option>
<option value="1.375">轻度活动(少量走动,5000~8000 步)</option>
<option value="1.55" selected>中度活动(每天约 1 万步)</option>
<option value="1.725">高度活动(体力工作 / 经常运动,1.5 万步以上)</option>
<option value="1.9">极高活动(专业运动员级别)</option>
</select>
</div>
<button class="calc-btn" onclick="calculate()">计算我的热量与营养素</button>
<div id="results">
<div class="bmi-badge" id="bmiBadge">
<div class="label">体质指数 BMI</div>
<div class="big" id="bmiVal">—</div>
<div class="status" id="bmiStatus">—</div>
</div>
<h2 class="sec">一、每日热量</h2>
<div class="result-grid">
<div class="stat">
<div class="k">基础代谢 BMR</div>
<div class="v" id="bmrVal">— <small>kcal</small></div>
</div>
<div class="stat">
<div class="k">每日维持热量 TDEE</div>
<div class="v" id="tdeeVal">— <small>kcal</small></div>
</div>
</div>
<div class="target">
<div class="t-title" id="targetTitle">建议每日摄入目标</div>
<div class="t-value" id="targetVal">— kcal</div>
<div class="t-desc" id="targetDesc">—</div>
</div>
<h2 class="sec">二、宏量营养素</h2>
<div class="macro">
<div class="m-card protein">
<div class="m-name">蛋白质</div>
<div class="m-val" id="proteinVal">—</div>
<div class="m-unit">g / 天</div>
<div class="m-sub" id="proteinSub">—</div>
</div>
<div class="m-card fat">
<div class="m-name">脂肪</div>
<div class="m-val" id="fatVal">—</div>
<div class="m-unit">g / 天</div>
<div class="m-sub" id="fatSub">—</div>
</div>
<div class="m-card carb">
<div class="m-name">碳水化合物</div>
<div class="m-val" id="carbVal">—</div>
<div class="m-unit">g / 天</div>
<div class="m-sub" id="carbSub">—</div>
</div>
</div>
<h2 class="sec">三、膳食纤维与饮水</h2>
<div class="two-col">
<div class="stat">
<div class="k">膳食纤维</div>
<div class="v" id="fiberVal">— <small>g</small></div>
</div>
<div class="stat">
<div class="k">饮水量</div>
<div class="v" id="waterVal">— <small>L</small></div>
</div>
</div>
<h2 class="sec">四、维生素与矿物质</h2>
<div><span class="pill" id="groupLabel">—</span></div>
<table class="nut">
<thead>
<tr><th>营养素</th><th style="text-align:right">每日推荐量</th><th>单位</th></tr>
</thead>
<tbody id="nutBody"></tbody>
</table>
<div class="note" id="formulaNote">—</div>
<div class="note warn" id="doctorNote" style="display:none;"></div>
</div>
</div>
</div>
<script>
let gender = 'male';
document.querySelectorAll('#genderSeg button').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('#genderSeg button').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
gender = btn.dataset.gender;
});
});
// 维生素与矿物质参考摄入量(接近中国 DRIs / 常用国际值)
const nutData = {
teenMale: { label: '青少年(14–17 岁)· 男', items: [
['钙', 1000, 'mg'], ['磷', 700, 'mg'], ['钾', 2000, 'mg'], ['钠', 1500, 'mg'],
['镁', 300, 'mg'], ['铁', 12, 'mg'], ['锌', 11.5, 'mg'], ['碘', 120, 'µg'],
['硒', 60, 'µg'], ['铜', 0.8, 'mg'],
['维生素 A', 800, 'µg RAE'], ['维生素 D', 10, 'µg'], ['维生素 E', 14, 'mg'],
['维生素 K', 75, 'µg'], ['维生素 C', 100, 'mg'], ['维生素 B1', 1.3, 'mg'],
['维生素 B2', 1.3, 'mg'], ['烟酸(B3)', 15, 'mg'], ['维生素 B6', 1.3, 'mg'],
['叶酸', 400, 'µg DFE'], ['维生素 B12', 2.4, 'µg']
]},
teenFemale: { label: '青少年(14–17 岁)· 女', items: [
['钙', 1000, 'mg'], ['磷', 700, 'mg'], ['钾', 2000, 'mg'], ['钠', 1500, 'mg'],
['镁', 250, 'mg'], ['铁', 18, 'mg'], ['锌', 8.5, 'mg'], ['碘', 120, 'µg'],
['硒', 60, 'µg'], ['铜', 0.8, 'mg'],
['维生素 A', 700, 'µg RAE'], ['维生素 D', 10, 'µg'], ['维生素 E', 14, 'mg'],
['维生素 K', 75, 'µg'], ['维生素 C', 100, 'mg'], ['维生素 B1', 1.1, 'mg'],
['维生素 B2', 1.1, 'mg'], ['烟酸(B3)', 12, 'mg'], ['维生素 B6', 1.2, 'mg'],
['叶酸', 400, 'µg DFE'], ['维生素 B12', 2.4, 'µg']
]},
adultMale: { label: '成人(18–49 岁)· 男', items: [
['钙', 800, 'mg'], ['磷', 720, 'mg'], ['钾', 2000, 'mg'], ['钠', 1500, 'mg'],
['镁', 330, 'mg'], ['铁', 12, 'mg'], ['锌', 12.5, 'mg'], ['碘', 120, 'µg'],
['硒', 60, 'µg'], ['铜', 0.8, 'mg'],
['维生素 A', 800, 'µg RAE'], ['维生素 D', 10, 'µg'], ['维生素 E', 14, 'mg'],
['维生素 K', 80, 'µg'], ['维生素 C', 100, 'mg'], ['维生素 B1', 1.4, 'mg'],
['维生素 B2', 1.4, 'mg'], ['烟酸(B3)', 15, 'mg'], ['维生素 B6', 1.4, 'mg'],
['叶酸', 400, 'µg DFE'], ['维生素 B12', 2.4, 'µg']
]},
adultFemale: { label: '成人(18–49 岁)· 女', items: [
['钙', 800, 'mg'], ['磷', 720, 'mg'], ['钾', 2000, 'mg'], ['钠', 1500, 'mg'],
['镁', 330, 'mg'], ['铁', 20, 'mg'], ['锌', 7.5, 'mg'], ['碘', 120, 'µg'],
['硒', 60, 'µg'], ['铜', 0.8, 'mg'],
['维生素 A', 700, 'µg RAE'], ['维生素 D', 10, 'µg'], ['维生素 E', 14, 'mg'],
['维生素 K', 80, 'µg'], ['维生素 C', 100, 'mg'], ['维生素 B1', 1.2, 'mg'],
['维生素 B2', 1.2, 'mg'], ['烟酸(B3)', 12, 'mg'], ['维生素 B6', 1.4, 'mg'],
['叶酸', 400, 'µg DFE'], ['维生素 B12', 2.4, 'µg']
]}
};
function getNutrientGroup(age, g) {
if (age < 18) return g === 'male' ? nutData.teenMale : nutData.teenFemale;
return g === 'male' ? nutData.adultMale : nutData.adultFemale;
}
function classifyBMI(bmi, age) {
if (age < 18) {
if (bmi < 15) return { text: '明显偏瘦(青少年参考)', color: '#dc2626', warn: true };
if (bmi < 16.5) return { text: '偏瘦(青少年参考)', color: '#f59e0b', warn: true };
if (bmi < 23.5) return { text: '大致正常(青少年参考)', color: '#059669', warn: false };
return { text: '偏高(青少年参考)', color: '#f59e0b', warn: false };
} else {
if (bmi < 18.5) return { text: '偏瘦', color: '#f59e0b', warn: true };
if (bmi < 24) return { text: '正常', color: '#059669', warn: false };
if (bmi < 28) return { text: '超重', color: '#f59e0b', warn: false };
return { text: '肥胖', color: '#dc2626', warn: false };
}
}
function round(n) { return Math.round(n); }
function calculate() {
const age = parseFloat(document.getElementById('age').value);
const heightCm = parseFloat(document.getElementById('height').value);
const weight = parseFloat(document.getElementById('weight').value);
const act = parseFloat(document.getElementById('activity').value);
if (!age || !heightCm || !weight) { alert('请完整填写年龄、身高和体重。'); return; }
const h = heightCm / 100;
const bmi = weight / (h * h);
// ---- BMR ----
let bmr, formulaUsed;
if (age < 18) {
if (gender === 'male') bmr = 17.5 * weight + 651;
else bmr = 12.2 * weight + 746;
formulaUsed = 'Schofield 青少年公式(' + (gender === 'male' ? '男' : '女') + ')';
} else {
if (gender === 'male') bmr = 10 * weight + 6.25 * heightCm - 5 * age + 5;
else bmr = 10 * weight + 6.25 * heightCm - 5 * age - 161;
formulaUsed = 'Mifflin-St Jeor 公式(成人)';
}
const tdee = bmr * act;
// ---- 目标热量 ----
const cls = classifyBMI(bmi, age);
const underweight = (age < 18 && bmi < 16.5) || (age >= 18 && bmi < 18.5);
const overweight = (age >= 18 && bmi >= 24);
let targetLow, targetHigh, targetTitle, targetDesc;
if (underweight) {
targetLow = round(tdee + 300); targetHigh = round(tdee + 500);
targetTitle = '建议每日摄入(健康增重)';
targetDesc = '在维持热量基础上 +300~500 kcal,让体重平稳回升;青春期还需额外营养支持生长。';
} else if (overweight) {
targetLow = round(tdee - 500); targetHigh = round(tdee - 300);
targetTitle = '建议每日摄入(温和减重)';
targetDesc = '在维持热量基础上 −300~500 kcal,温和减脂,不要低于基础代谢。';
} else {
targetLow = round(tdee - 100); targetHigh = round(tdee + 100);
targetTitle = '建议每日摄入(维持)';
targetDesc = '保持当前体重所需的每日热量,均衡饮食即可。';
}
const targetMid = (targetLow + targetHigh) / 2;
// ---- 宏量营养素 ----
let pLow, pHigh;
if (underweight || overweight) { pLow = 1.2; pHigh = 1.6; }
else { pLow = 1.0; pHigh = 1.4; }
const proteinLow = pLow * weight, proteinHigh = pHigh * weight;
const fatLow = targetMid * 0.25 / 9, fatHigh = targetMid * 0.30 / 9;
const carbMin = (targetMid - proteinHigh * 4 - fatHigh * 9) / 4;
const carbMax = (targetMid - proteinLow * 4 - fatLow * 9) / 4;
// ---- 纤维与饮水 ----
const fiber = targetMid / 1000 * 14;
const waterLow = weight * 30 / 1000;
const waterHigh = weight * 35 / 1000;
// ---- 渲染:热量 ----
document.getElementById('bmiVal').textContent = bmi.toFixed(1);
document.getElementById('bmiStatus').textContent = cls.text;
document.getElementById('bmiBadge').style.background =
'linear-gradient(135deg, ' + cls.color + ', ' + cls.color + 'cc)';
document.getElementById('bmrVal').innerHTML = round(bmr) + ' <small>kcal</small>';
document.getElementById('tdeeVal').innerHTML = round(tdee) + ' <small>kcal</small>';
document.getElementById('targetVal').textContent = targetLow + ' ~ ' + targetHigh + ' kcal';
document.getElementById('targetTitle').textContent = targetTitle;
document.getElementById('targetDesc').textContent = targetDesc;
// ---- 渲染:宏量 ----
document.getElementById('proteinVal').textContent = round(proteinLow) + '–' + round(proteinHigh);
document.getElementById('proteinSub').textContent = pLow + '–' + pHigh + ' g/kg 体重';
document.getElementById('fatVal').textContent = round(fatLow) + '–' + round(fatHigh);
document.getElementById('fatSub').textContent = '占总热量 25%–30%';
document.getElementById('carbVal').textContent = round(carbMin) + '–' + round(carbMax);
document.getElementById('carbSub').textContent = '扣除蛋白与脂肪后的剩余热量';
document.getElementById('fiberVal').innerHTML = round(fiber) + ' <small>g</small>';
document.getElementById('waterVal').innerHTML =
waterLow.toFixed(1) + '–' + waterHigh.toFixed(1) + ' <small>L</small>';
// ---- 渲染:微量营养素表 ----
const grp = getNutrientGroup(age, gender);
document.getElementById('groupLabel').textContent = grp.label;
const body = document.getElementById('nutBody');
body.innerHTML = '';
grp.items.forEach(function(it) {
const tr = document.createElement('tr');
tr.innerHTML = '<td>' + it[0] + '</td>' +
'<td class="num">' + it[1] + '</td>' +
'<td class="unit">' + it[2] + '</td>';
body.appendChild(tr);
});
// ---- 算法说明与提示 ----
document.getElementById('formulaNote').textContent =
'基础代谢:' + formulaUsed + ';总消耗 = 基础代谢 × 活动系数 ' + act + '。' +
'蛋白质与脂肪/碳水按比例分配,膳食纤维取每 1000 kcal 约 14 g,饮水按每公斤体重 30–35 ml。' +
'维生素与矿物质取自常见膳食参考摄入量,四舍五入到常用值。';
const dn = document.getElementById('doctorNote');
if (cls.warn && age < 18) {
dn.style.display = 'block';
dn.textContent = '⚠️ 重要提示:未成年人持续偏瘦,建议先到正规医院(儿科 / 青春发育门诊 / 内分泌科 / 临床营养科)做一次评估,排查是否存在需要处理的健康原因。本计算器结果仅供参考,不能替代医生或注册营养师的诊断与建议。';
} else {
dn.style.display = 'none';
}
document.getElementById('results').style.display = 'block';
document.getElementById('results').scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
window.addEventListener('DOMContentLoaded', calculate);
</script>
</body>
</html>