I have my feature to enlarge an image to full screen, which works for me in Google Chrome but not in other browsers.
I have the following code:
$("#Button1").click(function (ev) {
launchFullScreen(document.getElementById('imagen_test'), 'imagen_test');
});
function launchFullScreen(element, id_imagen) {
if (element.requestFullScreen) {
element.requestFullScreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
}
$('#' + id_imagen).addClass('aumenta_imagen');
}
.aumenta_imagen{
width:80% !important;
max-width:none !important;
height:100% !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img id="imagen_test" src="http://www.lorempixel.com/100/180/people" style="height:180px;width:100px;max-width:120px;" />
<input id="Button1" type="button" value="button" />
But when I try it in Mozilla, to enlarge the image to full screen, the following happens: It takes up the entire width and height, which makes the image look disproportionate.
I would like to know, what should I do so that the image can be seen in full screen also in Mozilla?
I was able to reproduce the problem. It happens because Chrome, Firefox, and IE/Edge all interpret full screen differently when applied to an image (although I have to search for sources that prove this point):
Chrome seems to respect CSS to some degree, while Firefox and IE/Edge ignore it outright and apply their own fullscreen image styling to it.
Bearing that in mind, I leave here two possible solutions (I like the second one better because it seems cleaner to me):
Option 1: Apply full screen to the body
You could apply full screen to the element
body
and then make the selected image have a few different styles. This way you avoid possible problems with Firefox and IE. Almost better if you also add a scrim (a darker curtain or middle layer to more visually separate the image from the rest of the content).For example, you could do something like this:
Option 2: Put the image as background
If instead of putting the image as an image (with a tag
img
) it is put as a backgrounddiv
(usingbackground-image
andbackground-size:contain
), when applying the full screen,div
it occupies the entire window but due to the size of the indicated background, it is displayed correctly.This would be an example of the code:
This second example doesn't look right because the iframe it runs in doesn't allow fullscreen. Copy the code to a page and run it locally to see the results.
There is an event called "fullscreenchange" that is fired when fullscreen mode starts or ends. Also using
document.fullscreenElement
(with the appropriate prefix) you know if you enter or exit full screen.Well therefore, simply store the value of
style
in a helper attribute (owner) and restore it when exiting full screen. I haven't tried it, but it's basically something like this: