Skip to main content

Display Battery Status using HTML, CSS and Javascript

    Hey everyone, welcome to Code Instinct! In this tutorial, we're going to show you how to create a battery status indicator using HTML, CSS and Javascript. This is a great Javascript project for beginners who want to learn how to use the Battery API Javascript and create a battery level indicator. We'll also be showing you how to add a battery charging animation and CSS battery shape to make it more visually appealing.Our tutorial will cover all the steps needed to create your own battery status indicator with ease. You'll learn how to use the Battery API Javascript to get the current battery level of your device, and display it using HTML and CSS. 

    We'll also be covering the different types of battery indicators you can use and how to customize them to fit your website design. You'll learn how to create a battery charging animation using Javascript and CSS to give your battery status indicator a more dynamic look.At Code Instinct, we're dedicated to providing Javascript tutorials for beginners, and this project is perfect for anyone who wants to learn more about Javascript and web development. 

    Whether you're looking to build your portfolio or just want to add a new skill to your resume, this battery level indicator project is a great way to get started.We offer a wide range of tutorials, from Javascript basics to more advanced coding techniques, so make sure to subscribe to our channel for more great content. So what are you waiting for? Let's start creating our battery level indicator using HTML, CSS and Javascript! Don't forget to hit that subscribe button to stay up-to-date with all our latest tutorials.

index.html


  <html>
      <head>
        <meta charset="UTF-8">
        <title>Battery Status</title>
        <link rel="stylesheet" href="style.css">
      </head>
      <body>
          <div class="container">
              <div class="battery-status">
                <div class="battery-level">
                  <div class="battery-fill"></div>
                  <span class="battery-percentage">100%</span>
                </div>
                <div class="battery-status-text">Plugged In</div>
              </div>
          </div>
          <script src="script.js"></script>
      </body>
  </html>



style.css

  body{
      font-family: sans-serif;
      background-color: #20d2df;
  }

  .container{
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
  }

  .battery-status{
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20px;
      background-color: #ffffff;
      border-radius: 10px;
      box-shadow: 0 20px 10px rgba(202,202,202,0.2);
  }

  .battery-level{
      position: relative;
      width: 150px;
      height: 60px;
      background-color: #e4e4e4;
      border-radius: 30px;
      overflow: hidden;
  }

  .battery-fill{
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      background-color: #00e09d;
      transition: width 0.5s ease;
  }

  .battery-percentage{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 24px;
      font-weight: bold;
      color: #f5f5f5;
  }

  .battery-status-text{
      margin-top: 10px;
      font-size: 20px;
      font-weight: bold;
      color: #a8a8a8;
  }


script.js

navigator.getBattery().then(function(battery)
{
    updateBatteryStatus(battery);
    battery.addEventListener('levelchange',function(){
        updateBatteryStatus(battery);
    });
    battery.addEventListener('chargingchange', function(){
        updateBatteryStatus(battery);
    });
});

function updateBatteryStatus(battery) {
    var batteryFill = document.querySelector(".battery-fill");
    var batteryPercentage = document.querySelector(".battery-percentage");
    var batteryStatusText = document.querySelector(".battery-status-text");

    var fillWidth = Math.round(battery.level * 100) + "%";
    batteryFill.style.width = fillWidth;
    batteryPercentage.innerHTML = fillWidth;

    if (battery.charging){
        batteryStatusText.innerHTML = "Now is Charging";
    } else {
        batteryStatusText.innerHTML = "Not Charging";
    }
}

Watch Full Tutorial Here! 

Comments

Popular posts from this blog

Auto Image Slider using CSS Only

     Are you ready to learn how to create an image slider using only CSS? Look no further! In this tutorial, we'll walk you through the step-by-step process of building a sleek and modern image slider using HTML and CSS. No JavaScript required! In this tutorial We'll cover setting up the HTML structure for the slider.       This will include creating the container element and styling the slider with CSS. You'll also learn how to add transitions and animations to create a smooth sliding effect. By the end of this tutorial, you'll have a fully functional image slider that you can use on your own website. Whether you're a beginner or an experienced developer, this tutorial has something for you. So let's get started creating your own image slider using CSS today! index.html <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/style.css"...

Control Your Weights using this Javascript App - Simple BMI Calculator

       Embark on a coding journey that will demystify the intricacies of web development with our comprehensive tutorial, "Creating a BMI Calculator Using JavaScript." Whether you're a novice in the realm of programming or an enthusiast seeking to augment your JavaScript skills, this step-by-step guide promises to be an illuminating experience. To kick off our journey, we'll delve into the fundamental building blocks of web development—HTML, CSS, and JavaScript. This tutorial caters to individuals ranging from absolute beginners to those looking to fortify their existing skill set. By providing a holistic understanding of these essential languages, participants can lay a solid foundation for their future endeavors in the dynamic field of web development.      As we progress through the tutorial, we'll focus extensively on JavaScript, unraveling its intricacies in a manner accessible to beginners. The comprehensive nature of this tutorial makes it an id...