I was able to reproduce the problem. It happens because Chrome, Firefox and IE / Edge interpret the whole screen differently when applied to an image (although I have to look for sources that show this point):
- In Chrome, the image enlarges proportionally until it reaches a stop (the height or width of the window).
- In Firefox and IE / Edge, the image is maximized by occupying 100% of the screen both horizontally and vertically (so the image looks stretched).
Chrome seems to respect the CSS to a certain extent, while Firefox and IE / Edge ignore it directly and apply its own full-screen image style.
With that in mind, I leave here two possible solutions (the second one I like more because it seems cleaner to me):
Option 1: Apply the full screen to the body
You could apply the full screen to the body
element and then make the selected image have different styles. This way you avoid possible problems with Firefox and IE. Almost better if you also add a scrim (a curtain or darker intermediate layer to more visually separate the image from the rest of the content).
For example, you could do something like this:
$("#Button1").click(function (ev) {
launchFullScreen(document.getElementById('body'), '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{
height: 100% !important;
width: auto !important;
max-width: 100% !important;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
z-index: 99999;
}
<body id="body">
<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" />
</body>
Option 2: Put the image as background
If instead of putting the image as an image (with a img
tag) it is set as the background of a div
(using background-image
and background-size:contain
), when applying the full screen, the div
it occupies the whole window but due to the size of the indicated background, it is displayed correctly.
This would be an example of the 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');
}
#imagen_test {
height:180px;
width:100px;
background-size:contain;
background-repeat:no-repeat;
background-position:center center;
}
.aumenta_imagen {
width:100% !important;
height:100% !important;
background-color:black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="imagen_test" style="background-image:url(http://www.lorempixel.com/100/180/people);"></div>
<input id="Button1" type="button" value="button" />
This second example does not look good because the iframe on which it runs does not allow full screen. Copy the code to a page and run it locally to see the results.