I use the code that exists in MDN and change it to switch full-screen mode, this means that when you click, you can enter the video in full-screen mode, if it is not so and vice versa.
document.addEventListener("keydown", function(e) { if (e.keyCode == 13) { toggleFullScreen(); } }, false); function toggleFullScreen() { var state = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen; if (!state) { if (document.documentElement.requestFullscreen) { document.documentElement.requestFullscreen(); } else if (document.documentElement.msRequestFullscreen) { document.documentElement.msRequestFullscreen(); } else if (document.documentElement.mozRequestFullScreen) { document.documentElement.mozRequestFullScreen(); } else if (document.documentElement.webkitRequestFullscreen) { document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); } } else { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } } }
You only need to call the toggleFullScreen() function when you click the buttons.
When I press Enter, it restarts the video. Why??
When you click on the buttons ( you focus on this button ), the video makes a full screen, and when you press re-enter the video output from full screen mode, and then the focused element (the button that has already been pressed ), press the button again to restart the video .
Now I understand what is happening. What is the solution?
You only need to call the blur() function to remove focus from the element.
HTML
<button onclick="playSprite(this,'full');">Play full video</button> <button onclick="playSprite(this,'tentotwenty');">Play from 10 to 20 seconds</button> <button onclick="playSprite(this,'tentothirty');">Play from 10 to thirty seconds</button> <button onclick="playSprite(this,'fiftytoonefifty');">Play from 50 to 200 seconds</button>
Javascript
function(currentElement,id) { currentElement.blur();
What is this ?
Each time the playSprite function is playSprite ( playSprite(this, YourdesireTime) ), the current element that clicked the element goes to the function to understand which button is pressed and remove focus from the clicked element.
What is the difference between your answer and @cviejo's answer?
My answer
- has a switching function
- don't reset video
- doesn't optimize (I think you don't like changing code)
@cviejo answer
Note. I don't mean to say that @cviejo's answer is not very good, because it really minimizes your code.
Conclusion
You can combine the code with the @cviejo code to get the best result.
Codepen