<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Laptop Charger Simulator</title>
<style>
body {
font-family: Arial, sans-serif;
background: #222;
color: #eee;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
h1 {
margin-bottom: 1rem;
}
#charger {
width: 300px;
height: 180px;
background: #444;
border-radius: 15px;
box-shadow: 0 0 30px #0f0 inset;
padding: 20px;
box-sizing: border-box;
position: relative;
}
#battery-container {
width: 200px;
height: 60px;
background: #111;
border: 3px solid #0f0;
border-radius: 8px;
overflow: hidden;
margin: 20px auto 10px auto;
position: relative;
}
#battery-level {
height: 100%;
background: #0f0;
width: 0%;
transition: width 0.2s ease;
}
#battery-tip {
width: 12px;
height: 30px;
background: #0f0;
position: absolute;
top: 15px;
right: -15px;
border-radius: 2px;
}
button {
background: #0a0;
color: #0f0;
border: 2px solid #0f0;
font-size: 1.2rem;
padding: 10px 25px;
margin: 5px;
border-radius: 8px;
cursor: pointer;
transition: background 0.3s ease;
}
button:hover:not(:disabled) {
background: #0f0;
color: #040;
}
button:disabled {
background: #222;
border-color: #444;
color: #555;
cursor: not-allowed;
}
#status {
margin-top: 10px;
font-size: 1.1rem;
font-weight: bold;
text-align: center;
height: 24px;
}
/* Glowing effect while charging */
#battery-level.charging {
box-shadow: 0 0 15px 5px #0f0;
}
</style>
</head>
<body>
<h1>Laptop Charger Simulator</h1>
<div id="charger" aria-label="Laptop Battery Charger">
<div id="battery-container" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" aria-label="Battery charge level">
<div id="battery-level"></div>
<div id="battery-tip"></div>
</div>
<div style="text-align:center;">
<button id="start-btn" aria-pressed="false">Start Charging</button>
<button id="stop-btn" disabled aria-pressed="false">Stop Charging</button>
</div>
<div id="status" aria-live="polite" aria-atomic="true">Battery at 0%</div>
</div>
<script>
(() => {
const batteryLevelEl = document.getElementById("battery-level");
const statusEl = document.getElementById("status");
const startBtn = document.getElementById("start-btn");
const stopBtn = document.getElementById("stop-btn");
const batteryContainer = document.getElementById("battery-container");
// We'll simulate battery level from 0 to 100%
// Because we can't actually charge hardware via a webpage.
// Charging interval references
let chargingInterval = null;
let batteryLevel = 0;
// Update battery UI and ARIA attributes
function updateBatteryUI(level) {
batteryLevelEl.style.width = level + "%";
batteryContainer.setAttribute("aria-valuenow", level);
statusEl.textContent = `Battery at ${level}%`;
if(level === 100) {
statusEl.textContent = "Battery Fully Charged!";
stopCharging();
}
}
function startCharging() {
if (chargingInterval) return;
startBtn.disabled = true;
stopBtn.disabled = false;
startBtn.setAttribute("aria-pressed", "true");
stopBtn.setAttribute("aria-pressed", "false");
batteryLevelEl.classList.add("charging");
chargingInterval = setInterval(() => {
if (batteryLevel < 100) {
batteryLevel++;
updateBatteryUI(batteryLevel);
} else {
stopCharging();
}
}, 200); // charge speed (5% per second)
statusEl.textContent = `Charging... Battery at ${batteryLevel}%`;
}
function stopCharging() {
if (!chargingInterval) return;
clearInterval(chargingInterval);
chargingInterval = null;
startBtn.disabled = false;
stopBtn.disabled = true;
startBtn.setAttribute("aria-pressed", "false");
stopBtn.setAttribute("aria-pressed", "true");
batteryLevelEl.classList.remove("charging");
statusEl.textContent = batteryLevel === 100 ? "Battery Fully Charged!" : `Charging stopped at ${batteryLevel}%`;
}
// Event listeners
startBtn.addEventListener("click", () => {
startCharging();
});
stopBtn.addEventListener("click", () => {
stopCharging();
});
// Initialize UI
updateBatteryUI(batteryLevel);
})();
</script>
</body>
</html>
Explanation:
-
What this does:
This is a fun simulation webpage that allows you to "start charging" and "stop charging" a battery graphic. Since websites cannot actually charge your laptop hardware, the battery level increases from 0 to 100% visually as you press Start, and stops when you press Stop or reach full charge.
-
Visuals:
There's a green battery bar that fills up gradually with a subtle glow while charging.
-
Accessibility:
ARIA roles and live regions update screen readers about the battery status.
-
Interactivity:
Start button disables once charging starts to prevent multiple intervals. Stop button is disabled when not charging.
Open this file in any modern browser and try clicking the Start and Stop buttons. The battery bar will fill visually simulating charging your laptop.