You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
6.6 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. # screenfull.js
  2. > 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.
  3. **This package is feature complete. No new changes will be accepted.**
  4. ---
  5. <p align="center"><b>🔥 Want to strengthen your core JavaScript skills and master ES6?</b><br>I would personally recommend this awesome <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.</p>
  6. ---
  7. ### [Demo](https://sindresorhus.com/screenfull.js)
  8. ### [Check out my other projects](https://github.com/sindresorhus?tab=repositories)
  9. ## Install
  10. Only 0.7 kB gzipped.
  11. Download the [production version][min] or the [development version][max].
  12. [min]: https://github.com/sindresorhus/screenfull.js/raw/gh-pages/dist/screenfull.min.js
  13. [max]: https://github.com/sindresorhus/screenfull.js/raw/gh-pages/dist/screenfull.js
  14. ```
  15. $ npm install --save screenfull
  16. ```
  17. Also available on [cdnjs](https://cdnjs.com/libraries/screenfull.js).
  18. ## Why?
  19. ### Screenfull
  20. ```js
  21. if (screenfull.enabled) {
  22. screenfull.request();
  23. }
  24. ```
  25. ### Vanilla JavaScript
  26. ```js
  27. document.fullscreenEnabled = document.fullscreenEnabled || document.mozFullScreenEnabled || document.documentElement.webkitRequestFullScreen;
  28. function requestFullscreen(element) {
  29. if (element.requestFullscreen) {
  30. element.requestFullscreen();
  31. } else if (element.mozRequestFullScreen) {
  32. element.mozRequestFullScreen();
  33. } else if (element.webkitRequestFullScreen) {
  34. element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
  35. }
  36. }
  37. if (document.fullscreenEnabled) {
  38. requestFullscreen(document.documentElement);
  39. }
  40. // Actually it's more if you want it to work in Safari, but let's not go there...
  41. ```
  42. ## Support
  43. [Supported browsers](http://caniuse.com/fullscreen)
  44. ## Documentation
  45. ### Examples
  46. #### Fullscreen the page
  47. ```js
  48. document.getElementById('button').addEventListener('click', () => {
  49. if (screenfull.enabled) {
  50. screenfull.request();
  51. } else {
  52. // Ignore or do something else
  53. }
  54. });
  55. ```
  56. #### Fullscreen an element
  57. ```js
  58. const el = document.getElementById('target');
  59. document.getElementById('button').addEventListener('click', () => {
  60. if (screenfull.enabled) {
  61. screenfull.request(el);
  62. }
  63. });
  64. ```
  65. #### Fullscreen an element with jQuery
  66. ```js
  67. const target = $('#target')[0]; // Get DOM element from jQuery collection
  68. $('#button').on('click', () => {
  69. if (screenfull.enabled) {
  70. screenfull.request(target);
  71. }
  72. });
  73. ```
  74. #### Toggle fullscreen on a image with jQuery
  75. ```js
  76. $('img').on('click', event => {
  77. if (screenfull.enabled) {
  78. screenfull.toggle(event.target);
  79. }
  80. });
  81. ```
  82. #### Detect fullscreen change
  83. ```js
  84. if (screenfull.enabled) {
  85. screenfull.on('change', () => {
  86. console.log('Am I fullscreen?', screenfull.isFullscreen ? 'Yes' : 'No');
  87. });
  88. }
  89. ```
  90. Remove listeners with:
  91. ```js
  92. screenfull.off('change', callback);
  93. ```
  94. #### Detect fullscreen error
  95. ```js
  96. if (screenfull.enabled) {
  97. screenfull.on('error', event => {
  98. console.error('Failed to enable fullscreen', event);
  99. });
  100. }
  101. ```
  102. See the [demo](https://sindresorhus.com/screenfull.js) for more examples, and view the source.
  103. #### Fullscreen an element with Angular.js
  104. You can use the [Angular.js binding](https://github.com/hrajchert/angular-screenfull) to do something like:
  105. ```html
  106. <div ngsf-fullscreen>
  107. <p>This is a fullscreen element</p>
  108. <button ngsf-toggle-fullscreen>Toggle fullscreen</button>
  109. </div>
  110. ```
  111. #### Fullscreen the page with Angular 2
  112. ```typescript
  113. import {Directive, HostListener} from '@angular/core';
  114. import * as screenfull from 'screenfull';
  115. @Directive({
  116. selector: '[toggleFullscreen]'
  117. })
  118. export class ToggleFullscreenDirective {
  119. @HostListener('click') onClick() {
  120. if (screenfull.enabled) {
  121. screenfull.toggle();
  122. }
  123. }
  124. }
  125. ```
  126. ```html
  127. <button toggleFullscreen>Toggle fullscreen<button>
  128. ```
  129. ### Methods
  130. #### .request()
  131. Make an element fullscreen.
  132. 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.
  133. If your page is inside an `<iframe>` you will need to add a `allowfullscreen` attribute (+ `webkitallowfullscreen` and `mozallowfullscreen`).
  134. Keep in mind that the browser will only enter fullscreen when initiated by user events like click, touch, key.
  135. #### .exit()
  136. Brings you out of fullscreen.
  137. #### .toggle()
  138. Requests fullscreen if not active, otherwise exits.
  139. #### .on(event, function)
  140. Events: `change` `error`
  141. Add a listener for when the browser switches in and out of fullscreen or when there is an error.
  142. #### .off(event, function)
  143. Remove a previously registered event listener.
  144. #### .onchange(function)
  145. Alias for `.on('change', function)`
  146. #### .onerror(function)
  147. Alias for `.on('error', function)`
  148. ### Properties
  149. #### .isFullscreen
  150. Returns a boolean whether fullscreen is active.
  151. #### .element
  152. Returns the element currently in fullscreen, otherwise `null`.
  153. #### .enabled
  154. 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`).
  155. #### .raw
  156. Exposes the raw properties (prefixed if needed) used internally: `requestFullscreen`, `exitFullscreen`, `fullscreenElement`, `fullscreenEnabled`, `fullscreenchange`, `fullscreenerror`
  157. ## FAQ
  158. ### How can I navigate to a new page when fullscreen?
  159. 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.
  160. ```js
  161. $('#new-page-btn').click(() => {
  162. const iframe = document.createElement('iframe')
  163. iframe.setAttribute('id', 'external-iframe');
  164. iframe.setAttribute('src', 'http://new-page-website.com');
  165. iframe.setAttribute('frameborder', 'no');
  166. iframe.style.position = 'absolute';
  167. iframe.style.top = '0';
  168. iframe.style.right = '0';
  169. iframe.style.bottom = '0';
  170. iframe.style.left = '0';
  171. iframe.style.width = '100%';
  172. iframe.style.height = '100%';
  173. $(document.body).prepend(iframe);
  174. document.body.style.overflow = 'hidden';
  175. });
  176. ```
  177. ## Resources
  178. - [Using the Fullscreen API in web browsers](http://hacks.mozilla.org/2012/01/using-the-fullscreen-api-in-web-browsers/)
  179. - [MDN - Fullscreen API](https://developer.mozilla.org/en/DOM/Using_full-screen_mode)
  180. - [W3C Fullscreen spec](http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html)
  181. - [Building an amazing fullscreen mobile experience](http://www.html5rocks.com/en/mobile/fullscreen/)
  182. ## License
  183. MIT © [Sindre Sorhus](https://sindresorhus.com)