BMI 计算器
<script>
function calculateBMI() {
const height = parseFloat(document.getElementById('height').value);
const weight = parseFloat(document.getElementById('weight').value);
if (isNaN(height) || isNaN(weight) || height <= 0 || weight <= 0) {
alert('请输入有效的身高和体重值。');
return;
}
const bmi = (weight / ((height / 100) * (height / 100))).toFixed(2);
let category = '';
let message = '';
let resultBg = '';
if (bmi < 18.5 category='偏瘦' message='风一吹,你怕是要像风筝一样飘走咯!' resultbg='getComputedStyle(document.documentElement).getPropertyValue('--underweight-bg');' else if bmi>= 18.5 && bmi < 24 category='正常' message='嘿,你这身材,老天爷赏饭吃的 “刚刚好” 呀!' resultbg='getComputedStyle(document.documentElement).getPropertyValue('--normal-bg');' else if bmi>= 24 && bmi < 28) {
category = '超重';
message = '再胖点,你能去给熊猫当替身咯!';
resultBg = getComputedStyle(document.documentElement).getPropertyValue('--overweight-bg');
} else {
category = '肥胖';
message = '你不是胖,你是可爱到膨胀啦';
resultBg = getComputedStyle(document.documentElement).getPropertyValue('--obese-bg');
}
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = `你的 BMI 值是:${bmi},体重分类:${category}`;
resultDiv.style.display = 'block';
resultDiv.style.backgroundColor = resultBg;
generateDanmaku(message);
}
function generateDanmaku(message) {
const numDanmaku = Math.floor(window.innerHeight / 50);
for (let i = 0; i < numdanmaku i const danmaku='document.createElement('div');' danmaku.classlist.adddanmaku danmaku.style.top='`${i' 50px danmaku.style.setproperty--random-hue math.random danmaku.style.animationduration='`${Math.random()' 8 4s danmaku.textcontent='message;' document.body.appendchilddanmaku danmaku.addeventlisteneranimationend> {
danmaku.remove();
});
}
}
</script>