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.
Only 0.7 KB gzipped (1.6 KB minified)
Download the production version or the development version.
if (screenfull.enabled) {
screenfull.request();
}
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);
}
// Actually it's more if you want it to work in Safari, but let's not go there...
- Chrome 15+
- Firefox 10+
- Safari 5.1+
- Opera 12.50+
Safari 5.1 doesn't support use of the keyboard in fullscreen.
document.getElementById('button').addEventListener('click', function() {
if (screenfull.enabled) {
screenfull.request();
} else {
// Ignore or do something else
}
});
var elem = document.getElementById('target');
document.getElementById('button').addEventListener('click', function() {
if (screenfull.enabled) {
screenfull.request(elem);
}
});
var target = $('#target')[0]; // Get DOM element from jQuery collection
$('#button').click(function() {
if (screenfull.enabled) {
screenfull.request(target);
}
});
$('img').click(function() {
if (screenfull.enabled) {
// We can use `this` since we want the clicked element
screenfull.toggle(this);
}
});
if (screenfull.enabled) {
screenfull.onchange = function() {
console.log('Am I fullscreen? ' + screenfull.isFullscreen ? 'Yes' : 'No');
};
}
See the demo for more examples, and view the source.
You can check for fullscreen support by checking the truthy/falsy value of screenfull
as done in the example above.
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.
Brings you out of fullscreen.
Requests fullscreen if not active, otherwise exits.
Override this method to get notified about fullscreen changes.
Override this method to get notified about fullscreen errors.
Returns a boolean whether fullscreen is active.
Returns the element currently in fullscreen, otherwise null
.
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
).
In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and minify your code using grunt.
Also, please don't edit files in the "dist" subdirectory as they are generated via grunt. You'll find source code in the "src" subdirectory!