I'm trying to find a better way to resize top banner images, creating different images that fit according the viewport orientation and viewport size without the image be "cutted" or distorced. I created the following code (css + js), and I would like to know if is the proper way, or if exist a better one.
CSS to <img>
or <div>
with background-image
:
max-width: 100%; width: auto; height: auto;
JS:
$(document).ready(function () {
resizeViewPort();
});
$(window).resize(function () {
resizeViewPort();
});
function resizeViewPort() {
alert(($(window).height() / $(window).width()).toFixed(2));
var height = $(window).height(), width = $(window).width();
var scale = (height / width).toFixed(2);
if (scale >= 1.7 && scale <= 1.8) {
if (height <= 736) { // 736x414
alert("Mobile PORTRAIT");
}
}
else if (scale >= 1.3 && scale <= 1.6) {
if (height <= 1024) { // 1024x768
alert("Tablet PORTRAIT");
}
}
else if (scale >= 0.6 && scale <= 0.8) {
if (height <= 768) { // 768x1024
alert("Tablet LANDSCAPE or Desktop 4:3");
} else if (height <= 900) { // 900x1440
alert("Desktop HD 16:10");
} else if (height <= 1200) { // 1200x1920
alert("Desktop FullHD 16:10");
}
}
else if (scale >= 0.5 && scale < 0.6) {
if (height <= 414) { // 414x736
alert("Mobile LANDSCAPE");
} else if (height <= 720) { // 720x1280
alert("Desktop HD 16:9");
} else if (height <= 1080) { // 1080x1920
alert("Desktop FullHD 16:9");
}
}
}
Using the solution of Saumil Soni, Germano Plebani and http://stephen.io/mediaqueries/, I reach the following solution (thank you all!):
With this solution you can upgrade to fits all resolutions you want. (For example, adding 1600x900 image to others desktop devices and 640x320 to new Galaxy Mobiles)