3

I have a video player with custom controls

<div>
<video poster="image.jpg" autoplay>
     <source src="video.mp4" id="vidM" type="video/mp4"></source>
</video>

<div id="media-controls">
<button type="button" id="play-pause" class="paused" >PLAY</button>
<button type="button" id="rewind10" >REWIND 10 SECONDS</button>
<button type="button" id="loop" >LOOP</button>
<input id="seekslider" type="range" min="0" max="100" value="0">
<span id="curtimetext">00:00</span> / <span id="durtimetext">00:00</span>
<button id="fullscreenbtn" >FS</button>
<button id="popoutbtn" >POPOUT</button>
<div id="fullVolume">
<button id="mutebtn" >MUTE</button>
<div id="volumeContainer"><input id="volumeslider" type="range" min="0" max="100" value="100" step="1" class="vertical" orient="vertical"></div>
</div>
</div>
</div>

and here are the custom controls for JUST the fullscreen Button:

var fullscreenbtn;
fullscreenbtn = document.getElementById("fullscreenbtn");
fullscreenbtn.addEventListener("click",toggleFullScreen,false);

function toggleFullScreen(){
    if ( document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement) {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.webkitExitFullscreen) {
            document.webkitExitFullscreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        }
        fullscreenbtn.setAttribute("class", "");
    } else{
        if ( document.fullscreenEnabled || document.webkitFullscreenEnabled || document.mozFullScreenEnabled || document.msFullscreenEnabled){
            if (player.requestFullscreen) {
                player.requestFullscreen();
            } else if (player.webkitRequestFullscreen) {
                player.webkitRequestFullscreen();
            } else if (player.mozRequestFullScreen) {
                player.mozRequestFullScreen();
            } else if (player.msRequestFullscreen) {
                player.msRequestFullscreen();
            }
            fullscreenbtn.setAttribute("class", "fullscreen");  
        } 
    }
}

Now here's my problem: When the user CLICKS the fullscreen button to toggle it on or off, the display of the button works just fine (I have some styles attached to the class "fullscreen"). But when a user clicks the ESCAPE button on their keyboard, the function toggles to disable fullscreen mode, but does NOT remove the 'fullscreen' class.

What do I need to add to my function to allow the toggle to still execute

Murphy1976
  • 1,415
  • 8
  • 40
  • 88
  • Its becuase you didnt click the button, you just fired an event (have a look into `fullscreenchange` event, allowing you to do the same `document.fullScreenElement` check. Check the answer here: http://stackoverflow.com/questions/10706070/how-to-detect-when-a-page-exits-fullscreen – somethinghere Mar 30 '15 at 14:10

1 Answers1

3

It's because you should be listening for the fullscreenchange event, not for a click, as escape is not a click event. Try the following:

document.addEventListener("fullscreenchange", function(event){
    if(!document.fullscreenElement){
        // remove your class here, clean up after full screen
    } 
}, false);

This event is prefixed for the moment, so you might need to check for prefixes, I usually use the following code to simplify it:

// We will have fullscreen as a variable holding all the prefix values we need
// Ifd its false, there is no support!
var fullscreen = false;
if(document.fullscreenEnabled) 
    fullscreen = {
        request: "requestFullscreen",
        element: "fullscreenElement",
        exit: "exitFullscreen",
        event: "fullscreenchange"
    };
else if(document.msfullscreenEnabled) 
    fullscreen = {
        request: "msRequestFullscreen",
        element: "msFullscreenElement",
        exit: "msExitFullscreen",
        event: "msfullscreenchange"
    };
else if(document.mozfullscreenEnabled) 
    fullscreen = {
        request: "mozRequestFullScreen",
        element: "mozFullScreenElement",
        exit: "mozCancelFullScreen",
        event: "mozfullscreenchange"
    };
else if(document.webkitFullscreenEnabled) 
    fullscreen = {
        request: "webkitRequestFullscreen",
        element: "webkitFullscreenElement",
        exit: "webkitExitFullscreen",
        event: "webkitfullscreenchange"
    };

Now you have an object that contains three things: the request function name, the document fullscreen element prefixed name and the exit name for the function. Now you can do the following:

if(fullscreen){
    document.addEventListener(fullscreen["event"], function(event){
        if(document[fullscreen["element"]]){
            document.body.style.backgroundColor = "green";
        } else {
            document.body.style.backgroundColor = "red";
        }
    }, false);
}

This way you could also reduce the code in your fullscreen function:

function toggleFullScreen(player){
    // start by checking if fullscreen was set. If not, don't continue.
    if(!fullscreen){
        return;
    }
    // Then check if an element is set and if the exit function exists 
    else if (document[fullscreen["element"]] && document[fullscreen["exit"]]) {
        document.document[fullscreen["exit"]]();
        fullscreenbtn.setAttribute("class", "");
    } 
    // otherwise check if request exists and trigger that.
    else {
        if (player[fullscreen["request"]]) {
            player[fullscreen["request"]]();
            fullscreenbtn.setAttribute("class", "fullscreen"); 
        }  
    }
}

You can find more here: https://developer.mozilla.org/en-US/docs/Web/Events/fullscreenchange And a previously (related) question here: How to detect when a page exits fullscreen?

The following snippet does not work in Stack Overflow, I'm unsure why. I have tested it and it works in my Safari 8, however.

var fullscreen = false;
if(document.fullscreenEnabled) 
    fullscreen = {
        request: "requestFullscreen",
        element: "fullscreenElement",
        exit: "exitFullscreen",
        event: "fullscreenchange"
    };
else if(document.msfullscreenEnabled) 
    fullscreen = {
        request: "msRequestFullscreen",
        element: "msFullscreenElement",
        exit: "msExitFullscreen",
        event: "msfullscreenchange"
    };
else if(document.mozfullscreenEnabled) 
    fullscreen = {
        request: "mozRequestFullScreen",
        element: "mozFullScreenElement",
        exit: "mozCancelFullScreen",
        event: "mozfullscreenchange"
    };
else if(document.webkitFullscreenEnabled) 
    fullscreen = {
        request: "webkitRequestFullscreen",
        element: "webkitFullscreenElement",
        exit: "webkitExitFullscreen",
        event: "webkitfullscreenchange"
    };


if(fullscreen){
    document.addEventListener(fullscreen["event"], function(event){
        if(document[fullscreen["element"]]){
            document.body.style.backgroundColor = "green";
        } else {
            document.body.style.backgroundColor = "red";
        }
    }, false);
}

function toggleFullScreen(player){
    // start by checking if fullscreen was set. If not, don't continue.
    if(!fullscreen){
        return;
    }
    // Then check if an element is set and if the exit function exists 
    else if (document[fullscreen["element"]] && document[fullscreen["exit"]]) {
        document.document[fullscreen["exit"]]();
        fullscreenbtn.setAttribute("class", "");
    } 
    // otherwise check if request exists and trigger that.
    else {
        if (player[fullscreen["request"]]) {
            player[fullscreen["request"]]();
            fullscreenbtn.setAttribute("class", "fullscreen"); 
        }  
    }
}
<video poster="image.jpg" autoplay>
     <source src="video.mp4" id="vidM" type="video/mp4"></source>
</video>

<button id="fullscreenbtn" onclick="toggleFullScreen(this)">FS</button>

The last bit (you asked in the comments) is that we would need to clean up your trigger a bit. I would try to unify the fullscreening of your player in one function, like the following:

function fullscreenEnableDisable(isFullScreen){
    // Check here to see if you need to enable or disable fullscreen classes
    // pass isFullScreen to force the change instead of check.
    isFullScreen = isFullScreen || typeof document[fullscreen["element"]] != "undefined";
    if(isFullScreen){
        // Do whatever you want related to being fullscreen
    } else {
        // Do whatever you want related to _not_ being fullscreen
    }
}

Now you can trigger fullscreenEnableDisable() when the fullscreenchange event is fired, and you can trigger fullscreenEnableDisable(true) to force it to apply your fullscreen stuff, and fullscreenEnableDisable(false) to force it to do you non-fullscreen stuff (these latter two are handy to use inside your button click).

Community
  • 1
  • 1
somethinghere
  • 16,311
  • 2
  • 28
  • 42
  • I have placed this function code, but nothing happens. Does it matter that my javascript is in an external file being called from the HTML? I also corrected your typo in the first !ddocument, so that isn't the issue. – Murphy1976 Mar 30 '15 at 14:29
  • @murphy1976 I've expanded a bit and noticed I forgot the prefixes and the event doesn't actually need the `on` word. Have a look and a try it out and let me know. – somethinghere Mar 30 '15 at 14:51
  • When I apply your code snippet, as is, to my functionality, I get two interesting results. The toggle back and forth between the states works great. I modified the document.background to only effect the style of the fullscreen button, so thank you for that. When I click the button, only the fulscreen button appears in the fullscreen view. It's just al black when the button in the center. Also, I am not able to click on the button once in fullscreen view. I can only click ESCAPE to exit fullscreen. – Murphy1976 Mar 30 '15 at 15:31
  • @Murphy1976 - thats because your video element has been fullscreened (as you were doing). You need to tell the trigger what to fullscreen, so try `toggleFullScreen(document.body)` to make your full body fullscreen, or pass the `toggleFullScreen()` function any other HTML element (if you have an id called `myVideoPlayer`, pass it something like `document.getElementById('myVideoPlayer')` including button etc.. Also, I amended my post with some info on how I'd solve your classes. – somethinghere Mar 30 '15 at 15:44
  • I focused the code to target the player, and I'm still getting the same errors: I cannot click on the button to bring it back, and now the class removal has reverted back as well. – Murphy1976 Mar 30 '15 at 17:31
  • You know, you've got to scale it back a bit if its not working. Try making it very focused and tweaking small bits until you know whats happening. The target for fullscreen should be the parent element containing everything. The function that sets everything up should be called when fullscreen changes, and then apply the correct change for the event (go fullscreen or exited?). I can't really help any more than this. I'd say have a read through the MDN docs, they prove very helpful. Then scale back and build up piece by piece... – somethinghere Mar 30 '15 at 18:13