<!
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
>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator {
background-color: #fff;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
text-align: center;
width: 320px;
}
h1 {
color: #2c3e50;
margin-bottom: 25px;
font-size: 26px;
}
.input-group {
margin: 15px 0;
text-align: left;
}
label {
display: block;
color: #666;
margin-bottom: 8px;
font-size: 14px;
}
input {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 6px;
font-size: 16px;
box-sizing: border-box;
transition: border-color 0.3s;
}
input:focus {
border-color: #3498db;
outline: none;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 12px 25px;
border-radius: 6px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
margin-top: 15px;
width: 100%;
}
button:hover {
background-color: #2980b9;
}
#result {
margin-top: 25px;
padding: 15px;
border-radius: 6px;
display: none;
}
.bmi-normal { background-color: #d4edda; color: #155724; }
.bmi-over { background-color: #fff3cd; color: #856404; }
.bmi-obese { background-color: #f8d7da; color: #721c24; }
.error { background-color: #f8d7da; color: #721c24; }
</
style
>
</
head
>
<
body
>
<
div
class
=
"calculator"
>
<
h1
>BMI 身体质量指数计算器</
h1
>
<
div
class
=
"input-group"
>
<
label
for
=
"height"
>身高 (cm)</
label
>
<
input
type
=
"number"
id
=
"height"
placeholder
=
"请输入身高"
step
=
"0.1"
>
</
div
>
<
div
class
=
"input-group"
>
<
label
for
=
"weight"
>体重 (kg)</
label
>
<
input
type
=
"number"
id
=
"weight"
placeholder
=
"请输入体重"
step
=
"0.1"
>
</
div
>
<
button
>立即计算</
button
>
<
div
id
=
"result"
></
div
>
</
div
>
<
script
>
function calculateBMI() {
// 获取输入值
const height = parseFloat(document.getElementById('height').value) / 100;
const weight = parseFloat(document.getElementById('weight').value);
// 验证输入
const resultDiv = document.getElementById('result');
if (!height || !weight || height <= 0 || weight <= 0) {
resultDiv.className = 'error';
resultDiv.innerHTML = '⚠️ 请输入有效的数值';
resultDiv.style.display = 'block';
return;
}
// 计算 BMI
const bmi = weight / (height * height);
const roundedBMI = bmi.toFixed(1);
// 结果分类
let category = '';
let className = '';
if (bmi <
18.5
) {
category
=
'体重过轻'
;
className
=
'bmi-normal'
;
} else if (bmi < 24) {
category
=
'正常范围'
;
className
=
'bmi-normal'
;
} else if (bmi < 28) {
category
=
'体重过重'
;
className
=
'bmi-over'
;
} else {
category
=
'肥胖'
;
className
=
'bmi-obese'
;
}
// 显示结果
resultDiv.className
= className;
resultDiv.innerHTML = `
<strong>BMI 指数:${roundedBMI}</
strong
><
br
>
${category}(${getAdvice(category)})
`;
resultDiv.style.display = 'block';
}
function getAdvice(category) {
const advice = {
'体重过轻': '建议适当增加营养摄入',
'正常范围': '继续保持健康的生活习惯',
'体重过重': '建议适当增加运动量',
'肥胖': '建议咨询专业医生制定健康计划'
};
return advice[category];
}
</
script
>
</
body
>
</
html
>