当前位置: 首页 > news >正文

就是这个样的粗爆,手搓一个计算器:热量计算器

       作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。

HTML:

<div class="calculator">
<label for="weight">体重 (kg):</label> 
<input id="weight" step="0.1" type="number" placeholder="请输入您的体重"> 
<label for="height">身高 (cm):</label> 
<input id="height" step="0.1" type="number" placeholder="请输入您的身高"> 
<label for="age">年龄:</label> 
<input id="age" type="number" placeholder="请输入您的年龄"> 
<label for="gender">性别:</label>
<select id="gender">
<option value="male">男性</option>
<option value="female">女性</option>
</select>
<label for="activity">活动水平:</label>
<select id="activity">
<option value="1.2">很少运动</option>
<option value="1.375">轻量运动</option>
<option value="1.55">中度运动</option>
<option value="1.725">高度运动</option>
<option value="1.9">非常高强度运动</option>
</select>
<button onclick="calculateCalories()">计算</button>
<div class="result">
<p>每日所需热量:</p>
<p id="calories">0</p>
<p>千卡</p>
</div>
</div>

JS:

function calculateCalories() {const weight = parseFloat(document.getElementById('weight').value);const height = parseFloat(document.getElementById('height').value);const age = parseInt(document.getElementById('age').value);const gender = document.getElementById('gender').value;const activity = parseFloat(document.getElementById('activity').value);if (isNaN(weight) || isNaN(height) || isNaN(age) || weight <= 0 || height <= 0 || age <= 0) {alert('请输入有效的数值');return;}let bmr = 0; // 基础代谢率if (gender === 'male') {bmr = 88.36 + (13.4 * weight) + (4.8 * height) - (5.7 * age);} else {bmr = 447.6 + (9.2 * weight) + (3.1 * height) - (4.3 * age);}const calories = bmr * activity; // 每日所需热量document.getElementById('calories').textContent = calories.toFixed(2);
}

CSS:

.calculator {width: 300px;background-color: #333;color: white;padding: 20px;border-radius: 10px;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}label {display: block;margin-bottom: 10px;font-size: 16px;
}input, select {width: 100%!important;padding: 10px!important;margin-bottom: 20px;color: #000000; border-radius: 5px;border: 1px solid #555;font-size: 16px!important;background-color: #ffffff!important;
}button {width: 100%;padding: 10px;background-color: #007bff;color: white;border: none;border-radius: 5px;cursor: pointer;font-size: 16px;
}button:hover {background-color: orange;
}.result {margin-top: 20px;text-align: center;
}option {background-color: #ffffff;
}p {font-size: 18px;margin-top: 5px!important;
}

  线上运行,可以直接打开:热量计算器


http://www.mrgr.cn/news/56798.html

相关文章:

  • ifftshift函数
  • Nacos相关问题
  • 如意控物联网项目(二)-ML307R模组软件调试之MQTT+硬件接口调试笔记
  • git 下载慢
  • 学习threejs,通过THREE.Raycaster给模型绑定点击事件
  • FFT过程中自动补零,补零部分FFT结果不为零
  • springboot高校在线健康咨询服务-计算机设计毕业源码86100
  • css实现外层不换行,内层换行
  • PVE虚拟机强制重启
  • ELK收集nginx日志
  • Stable Diffusion视频插件Ebsynth Utility安装方法
  • S32DS for ARM GPIO实践
  • 基于Spring Boot的论坛网站:从零到部署
  • TensorRT构建的engine文件用途
  • 自动化数据库管理:如何通过存储过程动态创建 MySQL 对象
  • 网盘与移动硬盘优势对比
  • 请求转发和重定向
  • 动态规划20:918. 环形子数组的最大和
  • 私募基金大屏粒子效果和动画闪烁效果 vue3+js
  • 4步教你绘制流程图,轻松提高工作效率!
  • PL/I语言的起源?有C语言,有B语言和A语言吗?为什么shell脚本最开始可能有#!/bin/bash字样?为什么不支持嵌套注释?
  • javascript实现sha256算法,支持分多次计算
  • 黑马javaWeb笔记重点备份11:Web请求与响应
  • 找剪辑中的BGM、音效,就上这8个网站
  • 想让服务端请求更高效?为什么说alova比axios更胜一筹?
  • C++11