1 | # screenfull.js
|
---|
2 |
|
---|
3 | > Simple wrapper for cross-browser usage of the JavaScript [Fullscreen API](https://developer.mozilla.org/en/DOM/Using_full-screen_mode), which lets you bring the page or any element into fullscreen. Smoothens out the browser implementation differences, so you don't have to.
|
---|
4 |
|
---|
5 | **[Not supported on iPhone](#support)**
|
---|
6 |
|
---|
7 | **This package is feature complete. No new features will be accepted.**
|
---|
8 |
|
---|
9 | #### [Demo](https://sindresorhus.com/screenfull.js)
|
---|
10 |
|
---|
11 | ## Install
|
---|
12 |
|
---|
13 | Only 0.7 kB gzipped.
|
---|
14 |
|
---|
15 | Download the [production version][min] or the [development version][max].
|
---|
16 |
|
---|
17 | [min]: https://github.com/sindresorhus/screenfull.js/raw/main/dist/screenfull.min.js
|
---|
18 | [max]: https://github.com/sindresorhus/screenfull.js/raw/main/dist/screenfull.js
|
---|
19 |
|
---|
20 | ```
|
---|
21 | $ npm install screenfull
|
---|
22 | ```
|
---|
23 |
|
---|
24 | Also available on [cdnjs](https://cdnjs.com/libraries/screenfull.js).
|
---|
25 |
|
---|
26 | ## Why?
|
---|
27 |
|
---|
28 | ### Screenfull
|
---|
29 |
|
---|
30 | ```js
|
---|
31 | if (screenfull.isEnabled) {
|
---|
32 | screenfull.request();
|
---|
33 | }
|
---|
34 | ```
|
---|
35 |
|
---|
36 | ### Vanilla JavaScript
|
---|
37 |
|
---|
38 | ```js
|
---|
39 | document.fullscreenEnabled =
|
---|
40 | document.fullscreenEnabled ||
|
---|
41 | document.mozFullScreenEnabled ||
|
---|
42 | document.documentElement.webkitRequestFullScreen;
|
---|
43 |
|
---|
44 | function requestFullscreen(element) {
|
---|
45 | if (element.requestFullscreen) {
|
---|
46 | element.requestFullscreen();
|
---|
47 | } else if (element.mozRequestFullScreen) {
|
---|
48 | element.mozRequestFullScreen();
|
---|
49 | } else if (element.webkitRequestFullScreen) {
|
---|
50 | element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (document.fullscreenEnabled) {
|
---|
55 | requestFullscreen(document.documentElement);
|
---|
56 | }
|
---|
57 |
|
---|
58 | // This is not even entirely comprehensive. There's more.
|
---|
59 | ```
|
---|
60 |
|
---|
61 | ## Support
|
---|
62 |
|
---|
63 | [Supported browsers](https://caniuse.com/#feat=fullscreen)
|
---|
64 |
|
---|
65 | **Note:** In order to use this package in Internet Explorer, you need a [`Promise` polyfill](https://github.com/stefanpenner/es6-promise).
|
---|
66 |
|
---|
67 | **Note:** Safari is supported on desktop and iPad, but not on iPhone. This is a limitation in the browser, not in Screenfull.
|
---|
68 |
|
---|
69 | ## Documentation
|
---|
70 |
|
---|
71 | ### Examples
|
---|
72 |
|
---|
73 | #### Fullscreen the page
|
---|
74 |
|
---|
75 | ```js
|
---|
76 | document.getElementById('button').addEventListener('click', () => {
|
---|
77 | if (screenfull.isEnabled) {
|
---|
78 | screenfull.request();
|
---|
79 | } else {
|
---|
80 | // Ignore or do something else
|
---|
81 | }
|
---|
82 | });
|
---|
83 | ```
|
---|
84 |
|
---|
85 | #### Fullscreen an element
|
---|
86 |
|
---|
87 | ```js
|
---|
88 | const element = document.getElementById('target');
|
---|
89 |
|
---|
90 | document.getElementById('button').addEventListener('click', () => {
|
---|
91 | if (screenfull.isEnabled) {
|
---|
92 | screenfull.request(element);
|
---|
93 | }
|
---|
94 | });
|
---|
95 | ```
|
---|
96 |
|
---|
97 | #### Hide navigation user-interface on mobile devices
|
---|
98 |
|
---|
99 | ```js
|
---|
100 | const element = document.getElementById('target');
|
---|
101 |
|
---|
102 | document.getElementById('button').addEventListener('click', () => {
|
---|
103 | if (screenfull.isEnabled) {
|
---|
104 | screenfull.request(element, {navigationUI: 'hide'});
|
---|
105 | }
|
---|
106 | });
|
---|
107 | ```
|
---|
108 |
|
---|
109 | #### Fullscreen an element with jQuery
|
---|
110 |
|
---|
111 | ```js
|
---|
112 | const element = $('#target')[0]; // Get DOM element from jQuery collection
|
---|
113 |
|
---|
114 | $('#button').on('click', () => {
|
---|
115 | if (screenfull.isEnabled) {
|
---|
116 | screenfull.request(element);
|
---|
117 | }
|
---|
118 | });
|
---|
119 | ```
|
---|
120 |
|
---|
121 | #### Toggle fullscreen on a image with jQuery
|
---|
122 |
|
---|
123 | ```js
|
---|
124 | $('img').on('click', event => {
|
---|
125 | if (screenfull.isEnabled) {
|
---|
126 | screenfull.toggle(event.target);
|
---|
127 | }
|
---|
128 | });
|
---|
129 | ```
|
---|
130 |
|
---|
131 | #### Detect fullscreen change
|
---|
132 |
|
---|
133 | ```js
|
---|
134 | if (screenfull.isEnabled) {
|
---|
135 | screenfull.on('change', () => {
|
---|
136 | console.log('Am I fullscreen?', screenfull.isFullscreen ? 'Yes' : 'No');
|
---|
137 | });
|
---|
138 | }
|
---|
139 | ```
|
---|
140 |
|
---|
141 | Remove listeners with:
|
---|
142 |
|
---|
143 | ```js
|
---|
144 | screenfull.off('change', callback);
|
---|
145 | ```
|
---|
146 |
|
---|
147 | #### Detect fullscreen error
|
---|
148 |
|
---|
149 | ```js
|
---|
150 | if (screenfull.isEnabled) {
|
---|
151 | screenfull.on('error', event => {
|
---|
152 | console.error('Failed to enable fullscreen', event);
|
---|
153 | });
|
---|
154 | }
|
---|
155 | ```
|
---|
156 |
|
---|
157 | See the [demo](https://sindresorhus.com/screenfull.js) for more examples, and view the source.
|
---|
158 |
|
---|
159 | #### Fullscreen an element with Angular.js
|
---|
160 |
|
---|
161 | You can use the [Angular.js binding](https://github.com/hrajchert/angular-screenfull) to do something like:
|
---|
162 |
|
---|
163 | ```html
|
---|
164 | <div ngsf-fullscreen>
|
---|
165 | <p>This is a fullscreen element</p>
|
---|
166 | <button ngsf-toggle-fullscreen>Toggle fullscreen</button>
|
---|
167 | </div>
|
---|
168 | ```
|
---|
169 |
|
---|
170 | #### Fullscreen the page with Angular 2
|
---|
171 |
|
---|
172 | ```ts
|
---|
173 | import {Directive, HostListener} from '@angular/core';
|
---|
174 | import screenfull = require('screenfull');
|
---|
175 |
|
---|
176 | @Directive({
|
---|
177 | selector: '[toggleFullscreen]'
|
---|
178 | })
|
---|
179 | export class ToggleFullscreenDirective {
|
---|
180 | @HostListener('click') onClick() {
|
---|
181 | if (screenfull.isEnabled) {
|
---|
182 | screenfull.toggle();
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 | ```
|
---|
187 |
|
---|
188 | ```html
|
---|
189 | <button toggleFullscreen>Toggle fullscreen<button>
|
---|
190 | ```
|
---|
191 |
|
---|
192 | ### API
|
---|
193 |
|
---|
194 | #### .request(element, options?)
|
---|
195 |
|
---|
196 | Make an element fullscreen.
|
---|
197 |
|
---|
198 | Accepts a DOM element and [`FullscreenOptions`](https://developer.mozilla.org/en-US/docs/Web/API/FullscreenOptions).
|
---|
199 |
|
---|
200 | The default element is `<html>`. If called with another element than the currently active, it will switch to that if it's a descendant.
|
---|
201 |
|
---|
202 | If your page is inside an `<iframe>` you will need to add a `allowfullscreen` attribute (+ `webkitallowfullscreen` and `mozallowfullscreen`).
|
---|
203 |
|
---|
204 | Keep in mind that the browser will only enter fullscreen when initiated by user events like click, touch, key.
|
---|
205 |
|
---|
206 | Returns a promise that resolves after the element enters fullscreen.
|
---|
207 |
|
---|
208 | #### .exit()
|
---|
209 |
|
---|
210 | Brings you out of fullscreen.
|
---|
211 |
|
---|
212 | Returns a promise that resolves after the element exits fullscreen.
|
---|
213 |
|
---|
214 | #### .toggle(element, options?)
|
---|
215 |
|
---|
216 | Requests fullscreen if not active, otherwise exits.
|
---|
217 |
|
---|
218 | Accepts a DOM element and [`FullscreenOptions`](https://developer.mozilla.org/en-US/docs/Web/API/FullscreenOptions).
|
---|
219 |
|
---|
220 | Returns a promise that resolves after the element enters/exits fullscreen.
|
---|
221 |
|
---|
222 | #### .on(event, function)
|
---|
223 |
|
---|
224 | Events: `'change' | 'error'`
|
---|
225 |
|
---|
226 | Add a listener for when the browser switches in and out of fullscreen or when there is an error.
|
---|
227 |
|
---|
228 | #### .off(event, function)
|
---|
229 |
|
---|
230 | Remove a previously registered event listener.
|
---|
231 |
|
---|
232 | #### .onchange(function)
|
---|
233 |
|
---|
234 | Alias for `.on('change', function)`
|
---|
235 |
|
---|
236 | #### .onerror(function)
|
---|
237 |
|
---|
238 | Alias for `.on('error', function)`
|
---|
239 |
|
---|
240 | #### .isFullscreen
|
---|
241 |
|
---|
242 | Returns a boolean whether fullscreen is active.
|
---|
243 |
|
---|
244 | #### .element
|
---|
245 |
|
---|
246 | Returns the element currently in fullscreen, otherwise `null`.
|
---|
247 |
|
---|
248 | #### .isEnabled
|
---|
249 |
|
---|
250 | 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`).
|
---|
251 |
|
---|
252 | #### .raw
|
---|
253 |
|
---|
254 | Exposes the raw properties (prefixed if needed) used internally: `requestFullscreen`, `exitFullscreen`, `fullscreenElement`, `fullscreenEnabled`, `fullscreenchange`, `fullscreenerror`
|
---|
255 |
|
---|
256 | ## FAQ
|
---|
257 |
|
---|
258 | ### How can I navigate to a new page when fullscreen?
|
---|
259 |
|
---|
260 | 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.
|
---|
261 |
|
---|
262 | ```js
|
---|
263 | $('#new-page-btn').click(() => {
|
---|
264 | const iframe = document.createElement('iframe')
|
---|
265 |
|
---|
266 | iframe.setAttribute('id', 'external-iframe');
|
---|
267 | iframe.setAttribute('src', 'https://new-page-website.com');
|
---|
268 | iframe.setAttribute('frameborder', 'no');
|
---|
269 | iframe.style.position = 'absolute';
|
---|
270 | iframe.style.top = '0';
|
---|
271 | iframe.style.right = '0';
|
---|
272 | iframe.style.bottom = '0';
|
---|
273 | iframe.style.left = '0';
|
---|
274 | iframe.style.width = '100%';
|
---|
275 | iframe.style.height = '100%';
|
---|
276 |
|
---|
277 | $(document.body).prepend(iframe);
|
---|
278 | document.body.style.overflow = 'hidden';
|
---|
279 | });
|
---|
280 | ```
|
---|
281 |
|
---|
282 | ## Resources
|
---|
283 |
|
---|
284 | - [Using the Fullscreen API in web browsers](https://hacks.mozilla.org/2012/01/using-the-fullscreen-api-in-web-browsers/)
|
---|
285 | - [MDN - Fullscreen API](https://developer.mozilla.org/en/DOM/Using_full-screen_mode)
|
---|
286 | - [W3C Fullscreen spec](https://fullscreen.spec.whatwg.org/)
|
---|
287 | - [Building an amazing fullscreen mobile experience](https://developers.google.com/web/fundamentals/native-hardware/fullscreen/)
|
---|