6f09117ba7222bd1f5fe3efdb3306eaa1bcd98a63578f1dfb3c9f1ce66cc67da2443703b137b8a3b7ab1fdb841f80c6e8fb41efe3a28e7914dd69f25349d12 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 features will be accepted.**
  4. #### [Demo](https://sindresorhus.com/screenfull.js)
  5. <br>
  6. ---
  7. <div align="center">
  8. <p>
  9. <p>
  10. <sup>
  11. <a href="https://github.com/sponsors/sindresorhus">My open source work is supported by the community</a>
  12. </sup>
  13. </p>
  14. <sup>Special thanks to:</sup>
  15. <br>
  16. <br>
  17. <a href="https://github.com/botpress/botpress">
  18. <img src="https://sindresorhus.com/assets/thanks/botpress-logo.svg" width="260" alt="Botpress">
  19. </a>
  20. <br>
  21. <sub><b>Botpress is an open-source conversational assistant creation platform.</b></sub>
  22. <br>
  23. <sub>They <a href="https://github.com/botpress/botpress/blob/master/.github/CONTRIBUTING.md">welcome contributions</a> from anyone, whether you're into machine learning,<br>want to get started in open-source, or just have an improvement idea.</sub>
  24. <br>
  25. </p>
  26. </div>
  27. ---
  28. <br>
  29. ## Install
  30. Only 0.7 kB gzipped.
  31. Download the [production version][min] or the [development version][max].
  32. [min]: https://github.com/sindresorhus/screenfull.js/raw/master/dist/screenfull.min.js
  33. [max]: https://github.com/sindresorhus/screenfull.js/raw/master/dist/screenfull.js
  34. ```
  35. $ npm install screenfull
  36. ```
  37. Also available on [cdnjs](https://cdnjs.com/libraries/screenfull.js).
  38. ## Why?
  39. ### Screenfull
  40. ```js
  41. if (screenfull.isEnabled) {
  42. screenfull.request();
  43. }
  44. ```
  45. ### Vanilla JavaScript
  46. ```js
  47. document.fullscreenEnabled =
  48. document.fullscreenEnabled ||
  49. document.mozFullScreenEnabled ||
  50. document.documentElement.webkitRequestFullScreen;
  51. function requestFullscreen(element) {
  52. if (element.requestFullscreen) {
  53. element.requestFullscreen();
  54. } else if (element.mozRequestFullScreen) {
  55. element.mozRequestFullScreen();
  56. } else if (element.webkitRequestFullScreen) {
  57. element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
  58. }
  59. }
  60. if (document.fullscreenEnabled) {
  61. requestFullscreen(document.documentElement);
  62. }
  63. // This is not even entirely comprehensive. There's more.
  64. ```
  65. ## Support
  66. [Supported browsers](http://caniuse.com/fullscreen)
  67. **Note:** In order to use this package in Internet Explorer, you need a [`Promise` polyfill](https://github.com/stefanpenner/es6-promise).
  68. **Note:** Safari is supported on desktop and iPad, but not on iPhone. This is a limitation in the browser, not in Screenfull.
  69. ## Documentation
  70. ### Examples
  71. #### Fullscreen the page
  72. ```js
  73. document.getElementById('button').addEventListener('click', () => {
  74. if (screenfull.isEnabled) {
  75. screenfull.request();
  76. } else {
  77. // Ignore or do something else
  78. }
  79. });
  80. ```
  81. #### Fullscreen an element
  82. ```js
  83. const element = document.getElementById('target');
  84. document.getElementById('button').addEventListener('click', () => {
  85. if (screenfull.isEnabled) {
  86. screenfull.request(element);
  87. }
  88. });
  89. ```
  90. #### Fullscreen an element with jQuery
  91. ```js
  92. const element = $('#target')[0]; // Get DOM element from jQuery collection
  93. $('#button').on('click', () => {
  94. if (screenfull.isEnabled) {
  95. screenfull.request(element);
  96. }
  97. });
  98. ```
  99. #### Toggle fullscreen on a image with jQuery
  100. ```js
  101. $('img').on('click', event => {
  102. if (screenfull.isEnabled) {
  103. screenfull.toggle(event.target);
  104. }
  105. });
  106. ```
  107. #### Detect fullscreen change
  108. ```js
  109. if (screenfull.isEnabled) {
  110. screenfull.on('change', () => {
  111. console.log('Am I fullscreen?', screenfull.isFullscreen ? 'Yes' : 'No');
  112. });
  113. }
  114. ```
  115. Remove listeners with:
  116. ```js
  117. screenfull.off('change', callback);
  118. ```
  119. #### Detect fullscreen error
  120. ```js
  121. if (screenfull.isEnabled) {
  122. screenfull.on('error', event => {
  123. console.error('Failed to enable fullscreen', event);
  124. });
  125. }
  126. ```
  127. See the [demo](https://sindresorhus.com/screenfull.js) for more examples, and view the source.
  128. #### Fullscreen an element with Angular.js
  129. You can use the [Angular.js binding](https://github.com/hrajchert/angular-screenfull) to do something like:
  130. ```html
  131. <div ngsf-fullscreen>
  132. <p>This is a fullscreen element</p>
  133. <button ngsf-toggle-fullscreen>Toggle fullscreen</button>
  134. </div>
  135. ```
  136. #### Fullscreen the page with Angular 2
  137. ```ts
  138. import {Directive, HostListener} from '@angular/core';
  139. import screenfull = require('screenfull');
  140. @Directive({
  141. selector: '[toggleFullscreen]'
  142. })
  143. export class ToggleFullscreenDirective {
  144. @HostListener('click') onClick() {
  145. if (screenfull.isEnabled) {
  146. screenfull.toggle();
  147. }
  148. }
  149. }
  150. ```
  151. ```html
  152. <button toggleFullscreen>Toggle fullscreen<button>
  153. ```
  154. ### API
  155. #### .request()
  156. Make an element fullscreen.
  157. 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.
  158. If your page is inside an `<iframe>` you will need to add a `allowfullscreen` attribute (+ `webkitallowfullscreen` and `mozallowfullscreen`).
  159. Keep in mind that the browser will only enter fullscreen when initiated by user events like click, touch, key.
  160. Returns a promise that resolves after the element enters fullscreen.
  161. #### .exit()
  162. Brings you out of fullscreen.
  163. Returns a promise that resolves after the element exits fullscreen.
  164. #### .toggle()
  165. Requests fullscreen if not active, otherwise exits.
  166. Returns a promise that resolves after the element enters/exits fullscreen.
  167. #### .on(event, function)
  168. Events: `'change' | 'error'`
  169. Add a listener for when the browser switches in and out of fullscreen or when there is an error.
  170. #### .off(event, function)
  171. Remove a previously registered event listener.
  172. #### .onchange(function)
  173. Alias for `.on('change', function)`
  174. #### .onerror(function)
  175. Alias for `.on('error', function)`
  176. #### .isFullscreen
  177. Returns a boolean whether fullscreen is active.
  178. #### .element
  179. Returns the element currently in fullscreen, otherwise `null`.
  180. #### .isEnabled
  181. 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`).
  182. #### .raw
  183. Exposes the raw properties (prefixed if needed) used internally: `requestFullscreen`, `exitFullscreen`, `fullscreenElement`, `fullscreenEnabled`, `fullscreenchange`, `fullscreenerror`
  184. ## FAQ
  185. ### How can I navigate to a new page when fullscreen?
  186. 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.
  187. ```js
  188. $('#new-page-btn').click(() => {
  189. const iframe = document.createElement('iframe')
  190. iframe.setAttribute('id', 'external-iframe');
  191. iframe.setAttribute('src', 'http://new-page-website.com');
  192. iframe.setAttribute('frameborder', 'no');
  193. iframe.style.position = 'absolute';
  194. iframe.style.top = '0';
  195. iframe.style.right = '0';
  196. iframe.style.bottom = '0';
  197. iframe.style.left = '0';
  198. iframe.style.width = '100%';
  199. iframe.style.height = '100%';
  200. $(document.body).prepend(iframe);
  201. document.body.style.overflow = 'hidden';
  202. });
  203. ```
  204. ## Resources
  205. - [Using the Fullscreen API in web browsers](http://hacks.mozilla.org/2012/01/using-the-fullscreen-api-in-web-browsers/)
  206. - [MDN - Fullscreen API](https://developer.mozilla.org/en/DOM/Using_full-screen_mode)
  207. - [W3C Fullscreen spec](http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html)
  208. - [Building an amazing fullscreen mobile experience](http://www.html5rocks.com/en/mobile/fullscreen/)