Last active
May 13, 2018 08:50
-
-
Save mckabue/8fbb1c7e5e81e8f89a99cecab8911e69 to your computer and use it in GitHub Desktop.
JavaScript Fullscreen Helper Method
This file contains 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
if (!Element.prototype.fullScreen) { | |
var full_screen = function (element) { | |
var self = this; | |
this.change = null; | |
var isFullScreen = this.isFullScreen = function () { | |
return (document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen || document.msFullscreenElement) ? true : false; | |
}; | |
this.requestFullScreen = function () { //https://stackoverflow.com/a/33768712 | |
// Supports most browsers and their versions. | |
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullscreen; | |
if (requestMethod) { // Native full screen. | |
requestMethod.call(element); | |
self.change(isFullScreen()); | |
} else if (typeof window.ActiveXObject !== "undefined") { // Older IE. | |
var wscript = new ActiveXObject("WScript.Shell"); | |
if (wscript !== null) { | |
wscript.SendKeys("{F11}"); | |
self.change(isFullScreen()); | |
} | |
} | |
}; | |
this.exitFullscreen = function () { | |
var exitMethod = document.fullscreenElement || document.webkitExitFullscreen || document.mozCancelFullScreen || document.msExitFullscreen; | |
if (exitMethod) { | |
exitMethod.call(document); | |
self.change(isFullScreen()); | |
} | |
}; | |
var _change = function (e) { | |
if (e.keyCode && e.keyCode !== 27) | |
return; | |
if (self.change) | |
self.change(isFullScreen()); | |
}; | |
this.dispose = function () { | |
document.removeEventListener("fullscreenchange", _change); | |
document.removeEventListener("mozfullscreenchange", _change); | |
document.removeEventListener("webkitfullscreenchange", _change); | |
document.removeEventListener("msfullscreenchange", _change); | |
document.removeEventListener("keyup", _change); | |
}; | |
this.dispose(); //dispose any previous events | |
document.addEventListener("fullscreenchange", _change); | |
document.addEventListener("mozfullscreenchange", _change); | |
document.addEventListener("webkitfullscreenchange", _change); | |
document.addEventListener("msfullscreenchange", _change); | |
document.addEventListener("keyup", _change); | |
}; | |
Object.defineProperty(Element.prototype, "fullScreen", { get: function () { return new full_screen(this); } }); | |
} | |
// USAGE | |
// https://jsfiddle.net/mckabue/yp9Lcxse/2/ | |
// var _fullScreen = self.$el.querySelector('.embed-responsive').fullScreen; | |
// _fullScreen.change = function (status) { | |
// console.log(status); | |
// }; | |
// if (_fullScreen.isFullScreen()) | |
// _fullScreen.exitFullscreen(); | |
// else | |
// _fullScreen.requestFullScreen(); | |
Author
mckabue
commented
May 13, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment