What is screenfull?
The screenfull npm package is a simple wrapper for cross-browser usage of the JavaScript Fullscreen API, which allows web pages to display web content in full-screen mode, exiting this mode, and toggling between these states. It abstracts away the differences between various browsers' implementations of the Fullscreen API, providing a consistent and easy-to-use interface for developers.
What are screenfull's main functionalities?
Toggle Fullscreen
This feature allows you to toggle fullscreen mode on and off for the page or a specific element. The code checks if fullscreen is supported and enabled, then toggles the fullscreen state.
if (screenfull.isEnabled) {
screenfull.toggle();
}
Enter Fullscreen
This feature enables entering fullscreen mode. The code sample demonstrates how to request the browser to enter fullscreen mode if it is supported.
if (screenfull.isEnabled) {
screenfull.request();
}
Exit Fullscreen
This feature allows you to exit fullscreen mode programmatically. The code checks if fullscreen is enabled and then exits fullscreen mode.
if (screenfull.isEnabled) {
screenfull.exit();
}
Fullscreen Change Event
This feature allows you to listen for changes in the fullscreen state. The code sample demonstrates how to register an event listener that logs the current fullscreen state whenever it changes.
if (screenfull.isEnabled) {
screenfull.on('change', () => {
console.log('Am I fullscreen?', screenfull.isFullscreen ? 'Yes' : 'No');
});
}
Other packages similar to screenfull
fscreen
fscreen is another abstraction library for the Fullscreen API, similar to screenfull. It provides a unified API for using the Fullscreen API across different browsers. Compared to screenfull, fscreen sticks closely to the original Fullscreen API specification, offering a more minimalistic approach.
bigscreen
bigscreen is a library that offers similar functionality to screenfull, with additional features such as the ability to use fullscreen on multiple monitors and more detailed event handling. It provides a more comprehensive solution for complex fullscreen scenarios but might be overkill for simple use cases.
screenfull.js
Simple wrapper for cross-browser usage of the JavaScript Fullscreen API, which lets you bring the page or any element into fullscreen. Smoothens out the browser implementation differences, so you don't have too.
Install
Only 0.7 kB gzipped.
Download the production version or the development version.
$ npm install --save screenfull
$ bower install --save screenfull
Why?
Screenfull
if (screenfull.enabled) {
screenfull.request();
}
Vanilla JavaScript
document.fullscreenEnabled = document.fullscreenEnabled || document.mozFullScreenEnabled || document.documentElement.webkitRequestFullScreen;
function requestFullscreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
}
}
if (document.fullscreenEnabled) {
requestFullscreen(document.documentElement);
}
Support
Supported browsers
Safari doesn't support use of the keyboard in fullscreen.
Documentation
Examples
Fullscreen the page
document.getElementById('button').addEventListener('click', function () {
if (screenfull.enabled) {
screenfull.request();
} else {
}
});
Fullscreen an element
var elem = document.getElementById('target');
document.getElementById('button').addEventListener('click', function () {
if (screenfull.enabled) {
screenfull.request(elem);
}
});
Fullscreen an element with jQuery
var target = $('#target')[0];
$('#button').click(function () {
if (screenfull.enabled) {
screenfull.request(target);
}
});
Toggle fullscreen on a image with jQuery
$('img').click(function () {
if (screenfull.enabled) {
screenfull.toggle(this);
}
});
Detect fullscreen change
if (screenfull.enabled) {
document.addEventListener(screenfull.raw.fullscreenchange, function () {
console.log('Am I fullscreen? ' + (screenfull.isFullscreen ? 'Yes' : 'No'));
});
}
Detect fullscreen error
if (screenfull.enabled) {
document.addEventListener(screenfull.raw.fullscreenerror, function (event) {
console.error('Failed to enable fullscreen', event);
});
}
See the demo for more examples, and view the source.
Methods
.request()
Make an element fullscreen.
Accepts a DOM element. Default is <html>
. If called with another element than the currently active, it will switch to that if it's a decendant.
If your page is inside an <iframe>
you will need to add a allowfullscreen
attribute (+ webkitallowfullscreen
and mozallowfullscreen
).
Keep in mind that the browser will only enter fullscreen when initiated by user events like click, touch, key.
.exit()
Brings you out of fullscreen.
.toggle()
Requests fullscreen if not active, otherwise exits.
Properties
.isFullscreen
Returns a boolean whether fullscreen is active.
.element
Returns the element currently in fullscreen, otherwise null
.
.enabled
Returns a boolean whether you are allowed to enter fullscreen. If your page is inside an <iframe>
you will need to add a allowfullscreen
attribute (+ webkitallowfullscreen
and mozallowfullscreen
).
.raw
Exposes the raw properties (prefixed if needed) used internally: requestFullscreen
, exitFullscreen
, fullscreenElement
, fullscreenEnabled
, fullscreenchange
, fullscreenerror
$(document).on(screenfull.raw.fullscreenchange, function () {
console.log('Fullscreen change');
});
Resources
License
MIT © Sindre Sorhus