前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【JS】BMI身体质量指数计算工具

【JS】BMI身体质量指数计算工具

作者头像
DevFrank
发布2024-07-24 15:55:09
590
发布2024-07-24 15:55:09
举报
文章被收录于专栏:C++开发学习交流

😏1. 知识介绍

BMI(Body Mass Index,身体质量指数),也称为体重指数,是一种常用的衡量成人人体肥胖程度的指标。它通过身高和体重之间的数值关系来评估一个人的体重是否适中。

BMI的计算公式如下:

代码语言:javascript
复制
BMI = 体重(kg)/ (身高(m) * 身高(m))

根据计算得到的BMI值,可以将人体的体重状况分为以下几个范围:

代码语言:javascript
复制
BMI < 18.5:体重过轻
18.5 <= BMI < 24:体重正常
24 <= BMI < 28:超重
BMI >= 28:肥胖

😊2. BMI计算工具的JS实现

效果如下:

在这里插入图片描述
在这里插入图片描述

代码示例:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
  <title>BMI Calculator</title>
  <style>
    /* create style container */
    .container {
      width: 300px;
      margin: 0 auto;
      padding: 20px;
      border: 1px solid #ccc;
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="container">
    <!-- container include header + label + input + button + result -->
    <h2>BMI Calculator</h2>
    <label for="weight">Weight (kg):</label>
    <input type="number" id="weight" min="0" step="0.01" required><br><br>
    <label for="height">Height (cm):</label>
    <input type="number" id="height" min="0" step="0.01" required><br><br>
    <button onclick="calculateBMI()">Calculate BMI</button><br><br>
    <div id="result"></div>
  </div>

  <script>
    // calculateBMI
    function calculateBMI() {
      var weightInput = document.getElementById("weight");
      var heightInput = document.getElementById("height");
      var resultDiv = document.getElementById("result");

      var weight = parseFloat(weightInput.value);
      var height = parseFloat(heightInput.value);

      // 检查输入是否有效
      if (isNaN(weight) || isNaN(height) || weight <= 0 || height <= 0) {
        resultDiv.innerHTML = "Please enter valid weight and height.";
      } else {
        // 将身高转换为米
        height = height / 100;
        // 计算BMI
        var bmi = weight / (height * height);
        // 显示结果
        resultDiv.innerHTML = "Your BMI is: " + bmi.toFixed(2);
      }
    }
  </script>
</body>
</html>

😆3. 黑白主题切换

晚上使用电脑时,如果还是这种纯白的页面,看起来很不舒服,因此有必要让网页根据时间来切换黑白主题,或者手动切换。

效果如下:

在这里插入图片描述
在这里插入图片描述

代码示例如下:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
  <title>BMI Calculator</title>
  <style>
    /* create style container */
    .container {
      width: 300px;
      margin: 0 auto;
      padding: 20px;
      border: 1px solid #ccc;
      text-align: center;
    }
    body {
      transition: background-color 0.5s ease;
      background-color: #f5f5f5; /* 默认白天背景色 */
      color: #333; /* 默认文本颜色 */
      text-align: center;
    }
    .container {
      width: 300px;
      margin: 0 auto;
      padding: 20px;
      border: 1px solid #ccc;
    }
    .toggle-btn {
      margin-top: 20px;
    }
  </style>
</head>
<body>
  <div class="container">
    <!-- container include header + label + input + button + result -->
    <h2>BMI Calculator</h2>
    <label for="weight">Weight (kg):</label>
    <input type="number" id="weight" min="0" step="0.01" required><br><br>
    <label for="height">Height (cm):</label>
    <input type="number" id="height" min="0" step="0.01" required><br><br>
    <button onclick="calculateBMI()">Calculate BMI</button><br><br>
    <div id="result"></div>
  </div>

  <div class="container">
    <h2>Day/Night Background</h2>
    <p>Current Time: <span id="time"></span></p>
    <button class="toggle-btn" onclick="toggleBackground()">Toggle Background</button>
  </div>

  <script>
    // calculateBMI
    function calculateBMI() {
      var weightInput = document.getElementById("weight");
      var heightInput = document.getElementById("height");
      var resultDiv = document.getElementById("result");

      var weight = parseFloat(weightInput.value);
      var height = parseFloat(heightInput.value);

      // 检查输入是否有效
      if (isNaN(weight) || isNaN(height) || weight <= 0 || height <= 0) {
        resultDiv.innerHTML = "Please enter valid weight and height.";
      } else {
        // 将身高转换为米
        height = height / 100;
        // 计算BMI
        var bmi = weight / (height * height);
        // 显示结果
        resultDiv.innerHTML = "Your BMI is: " + bmi.toFixed(2);
      }
    }

    // toggleBackground
    var timeDisplay = document.getElementById("time");
    var toggleButton = document.getElementsByClassName("toggle-btn")[0];
    
    function updateTime() {
      var date = new Date();
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();
      
      var time = hours + ":" + formatTimeComponent(minutes) + ":" + formatTimeComponent(seconds);
      timeDisplay.textContent = time;
      
      // 根据时间设置背景样式,按需要设置
      if (hours >= 18 || hours < 6) { // 如果是晚上(18:00 - 05:59)
        setNightBackground();
      } else { // 如果是白天(06:00 - 17:59)
        setDayBackground();
      }
    }
    
    function formatTimeComponent(component) {
      return component < 10 ? "0" + component : component;
    }
    
    function setDayBackground() {
      document.body.style.backgroundColor = "#f5f5f5";
      document.body.style.color = "#333";
    }
    
    function setNightBackground() {
      document.body.style.backgroundColor = "#333";
      document.body.style.color = "#f5f5f5";
    }
    
    function toggleBackground() {
      var currentColor = document.body.style.backgroundColor;
      if (currentColor === "rgb(245, 245, 245)") {
        setNightBackground();
      } else {
        setDayBackground();
      }
    }
    
    // 更新时间
    updateTime();
    setInterval(updateTime, 1000);
  </script>
</body>
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-11-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 😏1. 知识介绍
  • 😊2. BMI计算工具的JS实现
  • 😆3. 黑白主题切换
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档