最新在减肥,不知道减多少合适,想着制作一个计算公式,来计算体重
代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
<title>BIM 计算器</title>
<style>
/* 自定义过渡动画 */
.fade-in {
animation: fadeIn 0.5s ease-in-out;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
/* 输入框聚焦时的阴影效果 */
input[type="number"]:focus {
box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.5);
}
/* 按钮悬停和聚焦效果 */
button:hover {
transform: scale(1.05);
transition: transform 0.2s ease-in-out;
}
button:focus {
outline: none;
box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.5);
}
</style>
</head>
<body class="bg-gradient-to-r from-cyan-500 to-blue-500 font-sans flex justify-center items-center min-h-screen">
<div class="container mx-auto p-4 md:p-8">
<div class="bg-white rounded-lg shadow-md p-6 md:p-8 space-y-8 animate-fade-in">
<h1 class="text-4xl font-bold text-center text-gray-800 bg-gradient-to-r from-cyan-500 to-blue-500 bg-clip-text text-transparent">
BIM 计算器
</h1>
<form id="bimForm" class="space-y-6">
<div>
<label for="weight" class="block text-sm font-medium text-gray-700 mb-1">体重 (kg)</label>
<input type="number" id="weight" name="weight" step="0.1"
class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:border-blue-500 sm:text-sm">
</div>
<div>
<label for="height" class="block text-sm font-medium text-gray-700 mb-1">身高 (cm)</label>
<input type="number" id="height" name="height" step="0.1"
class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:border-blue-500 sm:text-sm">
</div>
<div>
<label for="gender" class="block text-sm font-medium text-gray-700 mb-1">性别</label>
<select id="gender" name="gender"
class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:border-blue-500 sm:text-sm">
<option value="female">女性</option>
<option value="male">男性</option>
</select>
</div>
<div class="flex justify-center">
<button type="submit"
class="px-6 py-3 bg-gradient-to-r from-cyan-500 to-blue-500 text-white rounded-md shadow-md hover:bg-gradient-to-r from-cyan-600 to-blue-600 focus:outline-none">
计算 BIM
</button>
</div>
</form>
<div id="result" class="text-center mt-6 fade-in">
<p id="bimValue" class="text-2xl font-bold text-gray-800"></p>
<p id="bimCategory" class="text-lg text-gray-600"></p>
<p id="normalWeight" class="text-lg text-gray-600"></p>
<p id="niceWeight" class="text-lg text-gray-600"></p>
</div>
</div>
</div>
<script>
const form = document.getElementById('bimForm');
const bimValueElement = document.getElementById('bimValue');
const bimCategoryElement = document.getElementById('bimCategory');
const normalWeightElement = document.getElementById('normalWeight');
const niceWeightElement = document.getElementById('niceWeight');
form.addEventListener('submit', function (e) {
e.preventDefault();
const weight = parseFloat(document.getElementById('weight').value);
const height = parseFloat(document.getElementById('height').value) / 100;
const gender = document.getElementById('gender').value;
if (!isNaN(weight) && !isNaN(height) && weight > 0 && height > 0) {
const bim = weight / (height * height);
let category;
if (bim < 18.5) {
category = '体重过轻';
} else if (bim >= 18.5 && bim < 24) {
category = '正常范围';
} else if (bim >= 24 && bim < 28) {
category = '超重';
} else {
category = '肥胖';
}
bimValueElement.textContent = `你的 BIM 值是: ${bim.toFixed(2)}`;
bimCategoryElement.textContent = `你的体重状况是: ${category}`;
const minNormalWeight = 18.5 * height * height;
const maxNormalWeight = 24 * height * height;
normalWeightElement.textContent = `正常体重范围是: ${minNormalWeight.toFixed(2)} kg - ${maxNormalWeight.toFixed(2)} kg`;
let minNiceWeight, maxNiceWeight;
if (gender === 'female') {
minNiceWeight = 18.5 * height * height;
maxNiceWeight = 22.2 * height * height;
} else {
minNiceWeight = 20 * height * height;
maxNiceWeight = 24 * height * height;
}
niceWeightElement.textContent = `正常好看的体重范围是: ${minNiceWeight.toFixed(2)} kg - ${maxNiceWeight.toFixed(2)} kg`;
} else {
bimValueElement.textContent = '请输入有效的体重和身高数值。';
bimCategoryElement.textContent = '';
normalWeightElement.textContent = '';
niceWeightElement.textContent = '';
}
});
</script>
</body>
</html>
1016


&spm=1001.2101.3001.11974&articleId=147786633&d=1&t=3&u=07b529bb4a074ec880892476e15f02d1)

被折叠的 条评论
为什么被折叠?



