GithubHelp home page GithubHelp logo

doc22940 / screenfull.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sindresorhus/screenfull

1.0 1.0 0.0 151 KB

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

Home Page: https://sindresorhus.com/screenfull.js

License: MIT License

JavaScript 40.16% HTML 49.48% TypeScript 10.35%

screenfull.js's Introduction

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.

This package is feature complete. No new features will be accepted.



My open source work is supported by the community

Special thanks to:

Botpress
Botpress is an open-source conversational assistant creation platform.
They welcome contributions from anyone, whether you're into machine learning,
want to get started in open-source, or just have an improvement idea.



Install

Only 0.7 kB gzipped.

Download the production version or the development version.

$ npm install screenfull

Also available on cdnjs.

Why?

Screenfull

if (screenfull.isEnabled) {
	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);
}

// This is not even entirely comprehensive. There's more.

Support

Supported browsers

Note: In order to use this package in Internet Explorer, you need a Promise polyfill.

Note: Safari is supported on desktop and iPad, but not on iPhone. This is a limitation in the browser, not in Screenfull.

Documentation

Examples

Fullscreen the page

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

Fullscreen an element

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

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

Fullscreen an element with jQuery

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

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

Toggle fullscreen on a image with jQuery

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

Detect fullscreen change

if (screenfull.isEnabled) {
	screenfull.on('change', () => {
		console.log('Am I fullscreen?', screenfull.isFullscreen ? 'Yes' : 'No');
	});
}

Remove listeners with:

screenfull.off('change', callback);

Detect fullscreen error

if (screenfull.isEnabled) {
	screenfull.on('error', 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>

Fullscreen the page with Angular 2

import {Directive, HostListener} from '@angular/core';
import screenfull = require('screenfull');

@Directive({
	selector: '[toggleFullscreen]'
})
export class ToggleFullscreenDirective {
	@HostListener('click') onClick() {
		if (screenfull.isEnabled) {
			screenfull.toggle();
		}
	}
}
<button toggleFullscreen>Toggle fullscreen<button>

API

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

Returns a promise that resolves after the element enters fullscreen.

.exit()

Brings you out of fullscreen.

Returns a promise that resolves after the element exits fullscreen.

.toggle()

Requests fullscreen if not active, otherwise exits.

Returns a promise that resolves after the element enters/exits fullscreen.

.on(event, function)

Events: 'change' | 'error'

Add a listener for when the browser switches in and out of fullscreen or when there is an error.

.off(event, function)

Remove a previously registered event listener.

.onchange(function)

Alias for .on('change', function)

.onerror(function)

Alias for .on('error', function)

.isFullscreen

Returns a boolean whether fullscreen is active.

.element

Returns the element currently in fullscreen, otherwise null.

.isEnabled

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

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

screenfull.js's People

Contributors

sindresorhus avatar bendingbender avatar oskarrough avatar stormtrooper1859 avatar sidkh avatar s-h-a-d-o-w avatar ben-eb avatar yocontra avatar gfx avatar hrajchert avatar leocaseiro avatar oscargodson avatar paulgrock avatar jackplug avatar timdp avatar cakesifu avatar dheerajkumarmadaan avatar levy9527 avatar shinnn avatar thislooksfun avatar

Stargazers

Acampbell avatar

Watchers

 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.