How Do I Get Dynamically Fluid Images Depending On Browser Window Aspect Ratio?
This might not be a simple question, but I try my best. I have this example site: http://lotvonen.tumblr.com/ I have a little piece of javascript that automatically calculates the
Solution 1:
You could also get the aspect in javascript on a regular basis and then add a class to the body object that would specify if it was 4:3, widescreen, or portrait. Then make it run on an interval in case the window changes size.
Example
CSS
.43 img { width: auto; }
.widescreen img { width: 100%; }
.portrait img { height: 100%; }
JavaScript
var getAspect = function(){
var h = window.innerHeight;
var w = window.innerWidth;
var aspect = w / h;
var 43 = 4 / 3;
var cssClass = "";
if (aspect > 43) {
cssClass = "widescreen";
}
else if (aspect === 43) {
cssClass = "43";
}
else {
cssClass = "portrait";
}
$("body").addClass(cssClass); // Using jQuery here, but it can be done without it
};
var checkAspect = setInterval(getAspect, 2000);
Solution 2:
I would suggest getting the aspect ratio first in javascript. Use window.innerHeight
and windows.innerWidth
, and make the necessary division. Then, make this a condition. When the screen in wider than its height, set the image in css to width: 100%
. Otherwise, set height: 100%
.
Post a Comment for "How Do I Get Dynamically Fluid Images Depending On Browser Window Aspect Ratio?"