Skip to content

Simple wrapper for cross-browser usage of the JavaScript Fullscreen API

License

Notifications You must be signed in to change notification settings

martingebhardt/screenfull.js

 
 

Repository files navigation

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 to.


🔥 Want to strengthen your core JavaScript skills and master ES6?
I would personally recommend this awesome ES6 course by Wes Bos.


Install

Only 0.7 kB gzipped.

Download the production version or the development version.

$ npm install --save screenfull

Also available on cdnjs.

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);
}

// Actually it's more if you want it to work in Safari, but let's not go there...

Support

Supported browsers

Safari doesn't support use of the keyboard in fullscreen.

Documentation

Examples

Fullscreen the page

document.getElementById('button').addEventListener('click', () => {
	if (screenfull.enabled) {
		screenfull.request();
	} else {
		// Ignore or do something else
	}
});

Fullscreen an element

const elem = document.getElementById('target');

document.getElementById('button').addEventListener('click', () => {
	if (screenfull.enabled) {
		screenfull.request(elem);
	}
});

Fullscreen an element with jQuery

const target = $('#target')[0]; // Get DOM element from jQuery collection

$('#button').on('click', () => {
	if (screenfull.enabled) {
		screenfull.request(target);
	}
});

Toggle fullscreen on a image with jQuery

$('img').on('click', event => {
	if (screenfull.enabled) {
		screenfull.toggle(event.target);
	}
});

Detect fullscreen change

if (screenfull.enabled) {
	document.addEventListener(screenfull.raw.fullscreenchange, () => {
		console.log('Am I fullscreen? ' + (screenfull.isFullscreen ? 'Yes' : 'No'));
	});
}

Detect fullscreen error

if (screenfull.enabled) {
	document.addEventListener(screenfull.raw.fullscreenerror, event => {
		console.error('Failed to enable fullscreen', event);
	});
}

See the demo for more examples, and view the source.

Fullscreen an element with Angular.js

You can use the Angular.js binding to do something like:

<div ngsf-fullscreen>
    <p>This is a fullscreen element</p>
    <button ngsf-toggle-fullscreen>Toggle fullscreen</button>
</div>

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, () => {
	console.log('Fullscreen change');
});

FAQ

How can I navigate to a new page when fullscreen?

That's not supported by browsers for security reasons. There is, however, a dirty workaround. Create a seamless iframe that fills the screen and navigate to the page in that instead.

$('#new-page-btn').click(() => {
	const iframe = document.createElement('iframe')

	iframe.setAttribute('id', 'external-iframe');
	iframe.setAttribute('src', 'http://new-page-website.com');
	iframe.setAttribute('frameborder', 'no');
	iframe.style.position = 'absolute';
	iframe.style.top = '0';
	iframe.style.right = '0';
	iframe.style.bottom = '0';
	iframe.style.left = '0';
	iframe.style.width = '100%';
	iframe.style.height = '100%';

	$(document.body).prepend(iframe);
	document.body.style.overflow = 'hidden';
});

Resources

License

MIT © Sindre Sorhus

About

Simple wrapper for cross-browser usage of the JavaScript Fullscreen API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 53.4%
  • HTML 46.6%