Last active
March 2, 2020 08:07
-
-
Save westc/9b2f88cfb085601e26e492908c039e5a to your computer and use it in GitHub Desktop.
Toggles fullscreen mode by checking which features are available.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @license Copyright 2020 - Chris West - MIT Licensed | |
* @see https://www.yourjs.com/blog/snippet-toggle-fullscreen/ | |
* Either toggles the fullscreen view on the document or a specified element or | |
* turns the fullscreen view on or off depending on the optional value passed | |
* in. | |
* @param opt_elem {Element|undefined|null} | |
* The element that should be targeted for fullscreen viewing. | |
* @param opt_value {?boolean=} | |
* Optional. If not given or undefined or null fullscreen will simply be | |
* toggled. If truthy the element will be set to fullscreen. If falsy and is | |
* not undefined or null fullscreen will be turned off if it is on. | |
* @returns {boolean|undefined} | |
* If possible returns a boolean indicating if upon calling the function, an | |
* element was in fullscreen mode. | |
*/ | |
function toggleFullscreen(opt_elem, opt_value) { | |
var doc = document, done, wasFull; | |
opt_elem = opt_elem || doc.documentElement; | |
'-frsexit ms-FRsExit moz-FRSCancel webkit-FRsExit'.replace( | |
/(\w*)-(f)(r)(s)(\w+)/gi, | |
function(_, prefix, f, r, s, exitWord) { | |
s = 'ull' + s + 'creen'; | |
if (!done && doc[prefix + f + s + 'Enabled']) { | |
// If in fullscreen mode and opt_value falsy. | |
if ((wasFull = !!doc[prefix + f + s + 'Element']) && doc[exitWord = prefix + exitWord + 'F' + s] && !opt_value) { | |
doc[done = exitWord](); | |
} | |
// If not in fullscreen mode and opt_value is truthy or opt_value is | |
// undefined or null. | |
else if (opt_elem[r = prefix + r + 'equestF' + s] && (opt_value || opt_value == null)) { | |
opt_elem[done = r](Element && Element.ALLOW_KEYBOARD_INPUT); | |
} | |
} | |
} | |
); | |
return wasFull; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be seen in action here:
https://www.yourjs.com/blog/snippet-toggle-fullscreen/