Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="page-wrapper">
  <h1>Battery Status API Demo</h1>

  <p id="msg"></p>

  <!-- Battery Level Meter -->
  <meter min="0" low="0.25" max="1" high="0.75" optimum="1" value="0" id="battery-meter"></meter>

  <!-- Battery Stats -->
  <table id="battery-stats">
    <thead>
      <tr>
        <th>Battery Level</th>
        <th>Charging Status</th>
        <th>Charging Time</th>
        <th>Discharging Time</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td id="battery-level"></td>
        <td id="battery-status"></td>
        <td id="battery-charging"></td>
        <td id="battery-discharging"></td>
      </tr>
    </tbody>
  </table>

</div>
              
            
!

CSS

              
                *, *:before, *:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

html {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 100%;
  background: #333;
}

#page-wrapper {
  width: 640px;
  background: #FFFFFF;
  padding: 1em;
  margin: 1em auto;
  border-top: 5px solid #69c773;
  box-shadow: 0 2px 10px rgba(0,0,0,0.8);
}

h1 {
  margin-top: 0;
}

p {
  font-size: 0.9em;
}

meter {
  width: 100%;
  height: 60px;
  background: #E6E6E6;
}

table {
  margin-top: 1em;
  width: 100%;
  text-align: center;
}

button {
  display: inline-block;
  border-radius: 3px;
  border: none;
  font-size: 0.9rem;
  padding: 0.4rem 0.8em;
  background: #69c773;
  border-bottom: 1px solid #498b50;
  color: white;
  -webkit-font-smoothing: antialiased;
  font-weight: bold;
  margin: 0 0.25rem;
  text-align: center;
}

button:hover, button:focus {
  opacity: 0.75;
  cursor: pointer;
}

button:active {
  opacity: 1;
  box-shadow: 0 -3px 10px rgba(0, 0, 0, 0.1) inset;
}

              
            
!

JS

              
                // Get the BatteryManager.
var battery = navigator.battery || navigator.mozBattery || navigator.webkitBattery;

var msg = document.getElementById('msg');

// Get the battery meter.
var batteryMeter = document.getElementById('battery-meter');

// Get the data cells in the table.
var batteryLevel = document.getElementById('battery-level');
var batteryStatus = document.getElementById('battery-status');
var batteryCharging = document.getElementById('battery-charging');
var batteryDischarging = document.getElementById('battery-discharging');


// Update the battery level meter (and table value).
function updateBatteryLevel() {
	batteryMeter.value = battery.level;
	batteryLevel.innerHTML = (battery.level * 100) + '%';
}


// Update the battery charging status.
function updateBatteryStatus() {
	if (battery.charging) {
		batteryStatus.innerHTML = 'Charging';
	} else {
		batteryStatus.innerHTML = 'Not Charging';
	}
}


// Update the battery charging time value.
function updateBatteryChargingTime() {
	if (battery.chargingTime === Infinity) {
		// Not charging.
		batteryCharging.innerHTML = '-';
	} else {
		// Charging.
		batteryCharging.innerHTML = (battery.chargingTime / 60) + ' minutes';
	}
}


// Update the battery discharging time value.
function updateBatteryDischargingTime() {
	if (battery.dischargingTime === Infinity) {
		// Not discharging.
		batteryDischarging.innerHTML = '-';
	} else {
		// Discharging.
		batteryDischarging.innerHTML = (battery.dischargingTime / 60) + ' minutes';
	}
}


// Set initial values and set up event listeners.
function init() {
	// Set initial values.
	updateBatteryLevel();
	updateBatteryStatus();
	updateBatteryChargingTime();
	updateBatteryDischargingTime();

	// Set up event listeners.
	battery.onlevelchange = updateBatteryLevel;
	battery.onchargingchange = updateBatteryStatus;
	battery.onchargingtimechange = updateBatteryChargingTime;
	battery.ondischargingtimechange = updateBatteryDischargingTime;
}


// Quick check for browser compatibility.
if (battery) {
	// API Supported!
	msg.innerHTML = 'Your browser supports the Battery Status API.';

	// Initialize the app.
	init();
} else {
	// API not supported.
	msg.innerHTML = 'Your browser does not support the Battery Status API :(';
}
              
            
!
999px

Console