I have a custom video play with it's own controls. I have a script to make changes to the controls when the user enters and exists fullscreen. The problem is that when I exit fullscreen using the ESC key instead of the button, the style changes to the controls are not reverted back. I need to ensure that exiting fullscreen using ESC or button will result in the same behaviour.
CSS:
function toggleFullScreen() {
elem = document.getElementById("video_container");
var db = document.getElementById("defaultBar");
var ctrl = document.getElementById("controls");
if (!document.fullscreenElement && // alternative standard method
!document.mozFullScreenElement && !document.webkitFullscreenElement) { // current working methods
if (document.documentElement.requestFullscreen) {
ctrl.style.width = '50%';
ctrl.style.left = '25%';
full.firstChild.src = ('images/icons/unfull.png');
elem.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
full.firstChild.src = 'images/icons/unfull.png';
ctrl.style.width = '50%';
ctrl.style.left = '25%';
elem.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
ctrl.style.width = '50%';
ctrl.style.left = '25%';
full.firstChild.src = 'images/icons/unfull.png';
elem.webkitRequestFullscreen();
}
} else if (document.exitFullscreen) {
full.firstChild.src = 'images/icons/full.png';
ctrl.style.width = '100%';
ctrl.style.left = '0';
document.exitFullscreen();
}
else if (document.mozCancelFullScreen) {
full.firstChild.src = 'images/icons/full.png';
ctrl.style.width = '100%';
ctrl.style.left = '0';
document.mozCancelFullScreen();
}
else if (document.webkitCancelFullScreen) {
ctrl.style.width = '100%';
ctrl.style.left = '0';
full.firstChild.src = 'images/icons/full.png';
document.webkitCancelFullScreen();
}
}
On computer keyboards, the Esc key Esc (named Escape key in the international standard series ISO/IEC 9995) is a key used to generate the escape character (which can be represented as ASCII code 27 in decimal, Unicode U+001B, or Ctrl + [ ).
function keyPress (e) { if(e. key === "Escape") { // write your logic here. } }
The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .
More recent and much cleaner: use event.key
. No more arbitrary number codes!
document.addEventListener("keydown", function(event) {
const key = event.key; // Or const {key} = event; in ES6+
if (key === "Escape") {
// Do things
}
});
document.addEventListener("keydown", ({key}) => {
if (key === "Escape") // Do things
})
Mozilla Docs
Supported Browsers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With