Files
class07m04 ca425d462f 新增每日所需热量计算器
依据日常活动量(每天约 1 万步)估算每日所需的热量摄入。
2026-09-24 00:25:45 +00:00

332 lines
12 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, "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: 620px;
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: 130px; 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: 24px; display: none; }
.bmi-badge {
border-radius: 12px;
padding: 16px;
margin-bottom: 18px;
color: #fff;
}
.bmi-badge .big { font-size: 32px; 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; }
.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: 22px; font-weight: 800; color: #4f46e5; margin-top: 2px; }
.stat .v small { font-size: 12px; font-weight: 600; color: #94a3b8; }
.target {
margin-top: 16px;
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: 26px; font-weight: 800; color: #059669; margin: 4px 0; }
.target .t-desc { font-size: 13px; color: #065f46; }
.note {
margin-top: 18px;
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; }
.dist { font-size: 12px; color: #64748b; text-align: center; margin-top: 6px; }
@media (max-width: 480px) { .result-grid { grid-template-columns: 1fr; } }
</style>
</head>
<body>
<div class="card">
<div class="header">
<h1>每日所需热量计算器</h1>
<p>自动识别青少年 / 成人,估算基础代谢、维持热量与健康增重目标</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>
<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" id="targetBox">
<div class="t-title" id="targetTitle">建议每日摄入目标</div>
<div class="t-value" id="targetVal">— kcal</div>
<div class="t-desc" id="targetDesc">—</div>
</div>
<div class="dist">体重与身高对应的健康体重范围见下方说明</div>
<div class="note" id="healthyRange">—</div>
<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;
});
});
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 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) {
// Schofield 公式(青少年,按体重)
if (gender === 'male') bmr = 17.5 * weight + 651;
else bmr = 12.2 * weight + 746;
formulaUsed = 'Schofield 青少年公式(' + (gender === 'male' ? '男' : '女') + ')';
} else if (age < 30) {
// Mifflin-St Jeor(成人)
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 公式(成人)';
} 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);
let targetLow, targetHigh, targetTitle, targetDesc;
const underweight = (age < 18 && bmi < 16.5) || (age >= 18 && bmi < 18.5);
if (underweight) {
// 增重:维持 + 300~500
targetLow = Math.round(tdee + 300);
targetHigh = Math.round(tdee + 500);
targetTitle = '建议每日摄入(健康增重)';
targetDesc = '在维持热量基础上 +300~500 kcal,让体重以每周约 0.2~0.5 kg 平稳回升。青春期还需额外营养支持生长。';
} else if (age >= 18 && bmi >= 24) {
// 减重
targetLow = Math.round(tdee - 500);
targetHigh = Math.round(tdee - 300);
targetTitle = '建议每日摄入(温和减重)';
targetDesc = '在维持热量基础上 −300~500 kcal,温和减脂,不要低于基础代谢。';
} else {
// 维持
targetLow = Math.round(tdee - 100);
targetHigh = Math.round(tdee + 100);
targetTitle = '建议每日摄入(维持)';
targetDesc = '保持当前体重所需的每日热量,均衡饮食即可。';
}
// ---- 健康体重范围 ----
let lowW, highW, rangeText;
if (age < 18) {
lowW = (16.5 * h * h).toFixed(1);
highW = (23.5 * h * h).toFixed(1);
rangeText = '按青少年参考,身高 ' + heightCm + ' cm 的体重提示区间约 ' + lowW + '~' + highW + ' kg(精确判断请用同年龄性别 BMI 百分位曲线)。';
} else {
lowW = (18.5 * h * h).toFixed(1);
highW = (24 * h * h).toFixed(1);
rangeText = '按成人标准,身高 ' + heightCm + ' cm 的健康体重范围约 ' + lowW + '~' + highW + ' kg。';
}
// ---- 渲染 ----
document.getElementById('bmiVal').textContent = bmi.toFixed(1);
document.getElementById('bmiStatus').textContent = cls.text;
const badge = document.getElementById('bmiBadge');
badge.style.background = 'linear-gradient(135deg, ' + cls.color + ', ' + cls.color + 'cc)';
document.getElementById('bmrVal').innerHTML = Math.round(bmr) + ' <small>kcal</small>';
document.getElementById('tdeeVal').innerHTML = Math.round(tdee) + ' <small>kcal</small>';
document.getElementById('targetVal').textContent = targetLow + ' ~ ' + targetHigh + ' kcal';
document.getElementById('targetTitle').textContent = targetTitle;
document.getElementById('targetDesc').textContent = targetDesc;
document.getElementById('healthyRange').textContent = rangeText;
document.getElementById('formulaNote').textContent =
'基础代谢算法:' + formulaUsed + ';总消耗 = 基础代谢 × 活动系数 ' + act + '。1 万步等日常活动已包含在活动系数中,无需重复叠加。';
const doctorNote = document.getElementById('doctorNote');
if (cls.warn && age < 18) {
doctorNote.style.display = 'block';
doctorNote.textContent = '⚠️ 重要提示:未成年人持续偏瘦,建议先到正规医院(儿科 / 青春发育门诊 / 内分泌科 / 临床营养科)做一次评估,排查是否存在需要处理的健康原因。本计算器结果仅供参考,不能替代医生或注册营养师的诊断与建议。';
} else {
doctorNote.style.display = 'none';
}
document.getElementById('results').style.display = 'block';
document.getElementById('results').scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
// 默认自动算一次
window.addEventListener('DOMContentLoaded', calculate);
</script>
</body>
</html>