1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| <!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: "Helvetica Neue", sans-serif; background: linear-gradient(to right, #f9f9f9, #e2f0ff); padding: 30px; margin: 0; }
.container { max-width: 400px; margin: 0 auto; background: #fff; padding: 24px; border-radius: 16px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); }
h2 { text-align: center; margin-bottom: 24px; color: #0078d7; }
label { display: block; margin-top: 12px; font-weight: bold; }
input { width: 100%; padding: 10px; margin-top: 6px; border-radius: 8px; border: 1px solid #ccc; box-sizing: border-box; }
button { width: 100%; margin-top: 20px; padding: 12px; background-color: #0078d7; color: white; border: none; border-radius: 8px; font-size: 16px; cursor: pointer; }
button:hover { background-color: #005fa3; }
.result { margin-top: 20px; text-align: center; font-size: 18px; color: #333; } </style> </head> <body> <div class="container"> <h2>BMI 计算器</h2>
<label for="height">身高(厘米):</label> <input type="number" id="height" placeholder="请输入您的身高,如 170">
<label for="weight">体重(斤):</label> <input type="number" id="weight" placeholder="请输入您的体重,如 130">
<button onclick="calculateBMI()">开始计算</button>
<div class="result" id="result"></div> </div>
<script> function calculateBMI() { const height = parseFloat(document.getElementById("height").value); const weightJin = parseFloat(document.getElementById("weight").value); const resultEl = document.getElementById("result");
if (!height || !weightJin || height <= 0 || weightJin <= 0) { resultEl.textContent = "请输入正确的身高和体重!"; return; }
const weightKg = weightJin / 2; const heightM = height / 100; const bmi = weightKg / (heightM * heightM); let status = "";
if (bmi < 18.5) status = "过轻"; else if (bmi < 25) status = "正常"; else if (bmi < 28) status = "过重"; else if (bmi < 32) status = "肥胖"; else status = "严重肥胖";
resultEl.textContent = `您的 BMI 是 ${bmi.toFixed(2)},属于「${status}」`; } </script> </body> </html>
|