feat: 新增 BMI 计算器网页 (bmi-calculator.html)
本次提交的决策记录: 1. 交付形态:采用单文件 HTML(CSS/JS 全部内联、零外部依赖), 可直接在浏览器打开,也便于后续作为 artifact 复用与迭代。 2. 存放位置:放在仓库根目录 bmi-calculator.html,与既有 index.html / index-copy.html 并列,不覆盖、不改动任何现有文件。 3. 计算方式:BMI = 体重(kg) ÷ 身高(m)²;身高统一以 cm 输入后换算为 m, 避免用户自行换算单位出错。 4. 交互方式:选择“实时计算”(监听 input 事件)而非点击按钮提交, 减少一次操作,输入过程即可看到结果变化。 5. 判定标准:采用双标准且可切换,默认使用中国标准。 - 中国标准:偏瘦 <18.5 / 正常 18.5–23.9 / 超重 24–27.9 / 肥胖 ≥28 - WHO 国际标准:偏瘦 <18.5 / 正常 18.5–24.9 / 超重 25–29.9 / 肥胖 ≥30 理由是同一 BMI 在不同标准下的结论不同,切换按钮让用户按自身场景判断。 6. 可视化:使用 15–35 的渐变量表 + 随 BMI 移动的指针,并以颜色编码健康状态 (蓝=偏瘦 / 绿=正常 / 黄=超重 / 红=肥胖),让结果不止是一个数字。 7. 输入容错:非数字、零或负数时显示占位 “--” 并隐藏指针,不做除零或无效计算; 指针位置 clamp 在 0%–100%,超出量表范围也不会溢出。 8. 可用性细节:输入框使用 inputmode="decimal" 便于移动端弹出数字键盘; 隐藏 number 类型的原生上下微调箭头;支持 ↑/↓ 方向键按 0.1 步进微调。 9. 范围限定:本次仅提交 BMI 计算功能,未加入“理想体重范围”提示等扩展功能, 留待后续按需追加。
This commit is contained in:
@@ -0,0 +1,315 @@
|
||||
<!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 {
|
||||
margin: 0;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
font-family: "PingFang SC", "Microsoft YaHei", -apple-system, "Segoe UI", sans-serif;
|
||||
background: linear-gradient(135deg, #1e2a44 0%, #2c3e63 50%, #16233b 100%);
|
||||
color: #e8eefc;
|
||||
}
|
||||
.card {
|
||||
width: 100%;
|
||||
max-width: 460px;
|
||||
background: rgba(255,255,255,0.06);
|
||||
border: 1px solid rgba(255,255,255,0.14);
|
||||
border-radius: 20px;
|
||||
padding: 28px 26px 24px;
|
||||
box-shadow: 0 24px 60px rgba(0,0,0,0.45);
|
||||
backdrop-filter: blur(12px);
|
||||
}
|
||||
h1 {
|
||||
margin: 0 0 4px;
|
||||
font-size: 24px;
|
||||
letter-spacing: 1px;
|
||||
}
|
||||
.subtitle {
|
||||
margin: 0 0 22px;
|
||||
font-size: 13px;
|
||||
color: #9fb0d0;
|
||||
}
|
||||
.field { margin-bottom: 16px; }
|
||||
label {
|
||||
display: block;
|
||||
font-size: 13px;
|
||||
color: #b9c7e4;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
.input-wrap { position: relative; }
|
||||
input[type="number"] {
|
||||
width: 100%;
|
||||
padding: 13px 46px 13px 14px;
|
||||
font-size: 17px;
|
||||
color: #fff;
|
||||
background: rgba(10,18,34,0.65);
|
||||
border: 1px solid rgba(255,255,255,0.16);
|
||||
border-radius: 12px;
|
||||
outline: none;
|
||||
transition: border-color .2s, box-shadow .2s;
|
||||
-moz-appearance: textfield;
|
||||
}
|
||||
input[type="number"]::-webkit-outer-spin-button,
|
||||
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
|
||||
input[type="number"]:focus {
|
||||
border-color: #5b8cff;
|
||||
box-shadow: 0 0 0 3px rgba(91,140,255,0.25);
|
||||
}
|
||||
.unit {
|
||||
position: absolute;
|
||||
right: 14px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
font-size: 13px;
|
||||
color: #8b9dc0;
|
||||
pointer-events: none;
|
||||
}
|
||||
.standards {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin: 18px 0 6px;
|
||||
}
|
||||
.standards button {
|
||||
flex: 1;
|
||||
padding: 9px 6px;
|
||||
font-size: 13px;
|
||||
color: #a9bade;
|
||||
background: rgba(255,255,255,0.05);
|
||||
border: 1px solid rgba(255,255,255,0.14);
|
||||
border-radius: 10px;
|
||||
cursor: pointer;
|
||||
transition: all .2s;
|
||||
}
|
||||
.standards button:hover { color: #fff; border-color: rgba(255,255,255,0.3); }
|
||||
.standards button.active {
|
||||
color: #fff;
|
||||
background: linear-gradient(135deg, #3f6ff0, #6a4bf0);
|
||||
border-color: transparent;
|
||||
box-shadow: 0 6px 18px rgba(80,110,240,0.4);
|
||||
}
|
||||
.result {
|
||||
margin-top: 22px;
|
||||
padding: 20px 18px;
|
||||
border-radius: 16px;
|
||||
background: rgba(10,18,34,0.5);
|
||||
border: 1px solid rgba(255,255,255,0.1);
|
||||
text-align: center;
|
||||
}
|
||||
.bmi-value {
|
||||
font-size: 46px;
|
||||
font-weight: 700;
|
||||
line-height: 1;
|
||||
letter-spacing: 1px;
|
||||
transition: color .3s;
|
||||
}
|
||||
.bmi-label {
|
||||
margin-top: 8px;
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.bmi-hint {
|
||||
margin-top: 6px;
|
||||
font-size: 12.5px;
|
||||
color: #9fb0d0;
|
||||
line-height: 1.6;
|
||||
min-height: 18px;
|
||||
}
|
||||
.bar {
|
||||
position: relative;
|
||||
height: 10px;
|
||||
margin: 20px 0 8px;
|
||||
border-radius: 6px;
|
||||
background: linear-gradient(90deg, #4aa8ff 0%, #35c98a 30%, #f0c33c 62%, #f0803c 80%, #e5484d 100%);
|
||||
overflow: visible;
|
||||
}
|
||||
.marker {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
border-radius: 50%;
|
||||
background: #fff;
|
||||
border: 3px solid #16233b;
|
||||
transform: translate(-50%, -50%);
|
||||
box-shadow: 0 2px 8px rgba(0,0,0,0.5);
|
||||
transition: left .35s cubic-bezier(.4,1.4,.6,1);
|
||||
left: 0;
|
||||
}
|
||||
.scale {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
font-size: 11px;
|
||||
color: #8b9dc0;
|
||||
}
|
||||
.scale span { transform: translateX(var(--nudge, 0)); }
|
||||
.legend {
|
||||
margin-top: 18px;
|
||||
font-size: 12px;
|
||||
color: #93a5c6;
|
||||
line-height: 1.75;
|
||||
}
|
||||
.legend b { color: #c8d6f0; font-weight: 600; }
|
||||
.empty .bmi-value { color: #5f6f8f; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<h1>BMI 计算器</h1>
|
||||
<p class="subtitle">输入身高和体重,实时查看身体质量指数</p>
|
||||
|
||||
<div class="field">
|
||||
<label for="height">身高</label>
|
||||
<div class="input-wrap">
|
||||
<input type="number" id="height" placeholder="例如 170" min="50" max="260" step="0.1" inputmode="decimal">
|
||||
<span class="unit">cm</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="weight">体重</label>
|
||||
<div class="input-wrap">
|
||||
<input type="number" id="weight" placeholder="例如 65" min="10" max="400" step="0.1" inputmode="decimal">
|
||||
<span class="unit">kg</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="standards">
|
||||
<button id="btnCn" class="active" type="button">中国标准</button>
|
||||
<button id="btnWho" type="button">WHO 国际标准</button>
|
||||
</div>
|
||||
|
||||
<div class="result empty" id="result">
|
||||
<div class="bmi-value" id="bmiValue">--</div>
|
||||
<div class="bmi-label" id="bmiLabel">等待输入</div>
|
||||
<div class="bmi-hint" id="bmiHint">请输入有效的身高与体重</div>
|
||||
</div>
|
||||
|
||||
<div class="bar">
|
||||
<div class="marker" id="marker" style="left:0; opacity:0;"></div>
|
||||
</div>
|
||||
<div class="scale">
|
||||
<span>15</span><span>18.5</span><span>24</span><span>28</span><span style="--nudge:-6px">35</span>
|
||||
</div>
|
||||
|
||||
<div class="legend" id="legend"></div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const heightEl = document.getElementById('height');
|
||||
const weightEl = document.getElementById('weight');
|
||||
const bmiValueEl = document.getElementById('bmiValue');
|
||||
const bmiLabelEl = document.getElementById('bmiLabel');
|
||||
const bmiHintEl = document.getElementById('bmiHint');
|
||||
const resultEl = document.getElementById('result');
|
||||
const markerEl = document.getElementById('marker');
|
||||
const legendEl = document.getElementById('legend');
|
||||
const btnCn = document.getElementById('btnCn');
|
||||
const btnWho = document.getElementById('btnWho');
|
||||
|
||||
const STANDARDS = {
|
||||
cn: {
|
||||
name: '中国标准',
|
||||
bands: [
|
||||
{ max: 18.5, label: '偏瘦', color: '#4aa8ff', hint: '体重偏低,建议注意营养摄入与均衡饮食。' },
|
||||
{ max: 24, label: '正常', color: '#35c98a', hint: '体重处于健康范围,继续保持良好的生活习惯。' },
|
||||
{ max: 28, label: '超重', color: '#f0c33c', hint: '体重偏高,建议增加运动并适当控制热量摄入。' },
|
||||
{ max: Infinity, label: '肥胖', color: '#e5484d', hint: '体重明显偏高,建议咨询医生或营养师制定计划。' }
|
||||
]
|
||||
},
|
||||
who: {
|
||||
name: 'WHO 国际标准',
|
||||
bands: [
|
||||
{ max: 18.5, label: 'Underweight', color: '#4aa8ff', hint: '体重偏低,建议注意营养摄入与均衡饮食。' },
|
||||
{ max: 25, label: 'Normal', color: '#35c98a', hint: '体重处于健康范围,继续保持良好的生活习惯。' },
|
||||
{ max: 30, label: 'Overweight', color: '#f0c33c', hint: '体重偏高,建议增加运动并适当控制热量摄入。' },
|
||||
{ max: Infinity, label: 'Obese', color: '#e5484d', hint: '体重明显偏高,建议咨询医生或营养师制定计划。' }
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
let standard = 'cn';
|
||||
|
||||
function buildLegend() {
|
||||
const bands = STANDARDS[standard].bands;
|
||||
const parts = bands.map(b => `<b style="color:${b.color}">■</b> ${b.label}`);
|
||||
legendEl.innerHTML = parts.join(' · ') +
|
||||
`<br>计算方式:BMI = 体重(kg) ÷ 身高(m)² (当前:${STANDARDS[standard].name})`;
|
||||
}
|
||||
|
||||
function getBand(bmi) {
|
||||
for (const b of STANDARDS[standard].bands) {
|
||||
if (bmi < b.max) return b;
|
||||
}
|
||||
return STANDARDS[standard].bands[STANDARDS[standard].bands.length - 1];
|
||||
}
|
||||
|
||||
function render() {
|
||||
const h = parseFloat(heightEl.value);
|
||||
const w = parseFloat(weightEl.value);
|
||||
|
||||
if (!isFinite(h) || !isFinite(w) || h <= 0 || w <= 0) {
|
||||
resultEl.classList.add('empty');
|
||||
bmiValueEl.textContent = '--';
|
||||
bmiLabelEl.textContent = '等待输入';
|
||||
bmiLabelEl.style.color = '#9fb0d0';
|
||||
bmiHintEl.textContent = '请输入有效的身高与体重';
|
||||
markerEl.style.opacity = '0';
|
||||
return;
|
||||
}
|
||||
|
||||
const bmi = w / Math.pow(h / 100, 2);
|
||||
if (!isFinite(bmi) || bmi <= 0) return;
|
||||
|
||||
const band = getBand(bmi);
|
||||
resultEl.classList.remove('empty');
|
||||
bmiValueEl.textContent = bmi.toFixed(1);
|
||||
bmiValueEl.style.color = band.color;
|
||||
bmiLabelEl.textContent = band.label;
|
||||
bmiLabelEl.style.color = band.color;
|
||||
bmiHintEl.textContent = band.hint;
|
||||
|
||||
// 定位指针:15 → 0%,35 → 100%
|
||||
const pct = Math.max(0, Math.min(100, ((bmi - 15) / 20) * 100));
|
||||
markerEl.style.left = pct + '%';
|
||||
markerEl.style.opacity = '1';
|
||||
}
|
||||
|
||||
function switchStandard(s) {
|
||||
standard = s;
|
||||
btnCn.classList.toggle('active', s === 'cn');
|
||||
btnWho.classList.toggle('active', s === 'who');
|
||||
buildLegend();
|
||||
render();
|
||||
}
|
||||
|
||||
heightEl.addEventListener('input', render);
|
||||
weightEl.addEventListener('input', render);
|
||||
btnCn.addEventListener('click', () => switchStandard('cn'));
|
||||
btnWho.addEventListener('click', () => switchStandard('who'));
|
||||
|
||||
// 键盘上下键微调
|
||||
[heightEl, weightEl].forEach(el => {
|
||||
el.addEventListener('keydown', e => {
|
||||
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
const step = parseFloat(el.step) || 1;
|
||||
const cur = parseFloat(el.value) || 0;
|
||||
el.value = (cur + (e.key === 'ArrowUp' ? step : -step)).toFixed(1);
|
||||
render();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
buildLegend();
|
||||
render();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user