GithubHelp home page GithubHelp logo

sirithink / screenfull.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sindresorhus/screenfull

0.0 2.0 0.0 268 KB

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

Home Page: sindresorhus.com/screenfull.js

screenfull.js's Introduction

screenfull.js

Views in the last 24 hours

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 (1.6 KB minified)

Manually

Download the production version or the development version.

bower install --save screenfull

npm install --save screenfull

//cdnjs.cloudflare.com/ajax/libs/screenfull.js/1.0.4/screenfull.min.js

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 5.1 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 {
		// Ignore or do something 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]; // Get DOM element from jQuery collection
$('#button').click(function () {
	if (screenfull.enabled) {
		screenfull.request(target);
	}
});

Toggle fullscreen on a image with jQuery

$('img').click(function () {
	if (screenfull.enabled) {
		// We can use `this` since we want the clicked element
		screenfull.toggle(this);
	}
});

Detect fullscreen change

if (screenfull.enabled) {
	document.addEventListener(screenfull.raw.fullscreenchange, 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.

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.

.onchange()

Override this method to get notified about fullscreen changes.

You should rather use a real event listener:

document.addEventListener(screenfull.raw.fullscreenchange, function () {});

.onerror()

Override this method to get notified about fullscreen errors.

You should rather use a real event listener:

document.addEventListener(screenfull.raw.fullscreenerror, function () {});

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

screenfull.js's People

Contributors

sindresorhus avatar oscargodson avatar paulgrock avatar jackplug avatar shinnn avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.