Skip to content

Instantly share code, notes, and snippets.

@simonewebdesign
Last active September 28, 2023 04:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonewebdesign/6174190 to your computer and use it in GitHub Desktop.
Save simonewebdesign/6174190 to your computer and use it in GitHub Desktop.
go fullscreen on click (cross-browser) - with a single button
$('.fullscreenbutton').on('click', function () {
function toggleFullScreen() {
if (!document.fullscreenElement && // alternative standard method
!document.mozFullScreenElement && !document.webkitFullscreenElement) { // current working methods
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.cancelFullScreen) {
document.cancelFullScreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
}
}
toggleFullScreen();
});
// change icons and position
function toggleState() {
$('.fullscreenbutton')
.toggleClass('enterfullscreen')
.toggleClass('exitfullscreen');
}
// hide header and footer
function toggleEverythingButArrows() {
$('#header').slideToggle('slow');
$('#footer').slideToggle('slow');
}
// event listeners
document.addEventListener("fullscreenchange", function () {
//document.fullscreen ? console.log(1) : console.log("no");
toggleState();
toggleEverythingButArrows();
}, false);
document.addEventListener("mozfullscreenchange", function () {
//document.mozFullScreen ? console.log("moz") : console.log("not moz");
toggleState();
toggleEverythingButArrows();
}, false);
document.addEventListener("webkitfullscreenchange", function () {
//document.webkitIsFullScreen ? console.log("webkit") : console.log("not webkit");
toggleState();
toggleEverythingButArrows();
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment