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
Post a Comment