0

I have found a code here on stackoverflow to check if the picture was portrait or landscape oriented, and add css width and height formatting.

After getting it to work (hence the $LSC everywhere), I found that it immediately jumps to "else", and outputs only that result. (all images get width auto and height 100%)

My question is, what should I change in the code to make it work and really compare width and height?

var $LSC = jQuery.noConflict(); $LSC(document).ready(function(){ $LSC(".pg-cat-image").each(function(){ var real_width = $LSC(this).width(); var real_height = $LSC(this).height(); var parentwidth = $LSC(this).parent().width(); var parentheight = $LSC(this).parent().height(); var ratioParent = parentwidth / parentheight; var ratio = real_width / real_height; if (ratioParent < ratio) { $LSC(this).css({'width' : '100%', 'height' : 'auto'}); }else{ $LSC(this).css({'width' : 'auto', 'height' : '100%'}); } }); }); 
3
  • Have you added a breakpoint and stepped through the code? Commented Mar 4, 2015 at 11:53
  • At the very least have you checked that ratio and ratioParent are what you expect them to be? Commented Mar 4, 2015 at 12:07
  • Added breakpoint, came up with Uncaught ReferenceError: real_width is not defined. Added user333216's change, and it looks like it needed to wait after the load event. Also used Stian's change to the code, for a cleaner code, now it works perfectly (even for squares :) ) Commented Mar 4, 2015 at 16:45

2 Answers 2

2

I don't understand why you're involving the parent container. Can't you just compare the image's width with it's height?

var $LSC = jQuery.noConflict(); $LSC(document).ready(function(){ $LSC(".pg-cat-image").each(function(){ var real_width = $LSC(this).width(); var real_height = $LSC(this).height(); if (real_width > real_height) { // The image is landscape } else if (real_width < real_height) { // The image is portrait } else { // The image is square } }); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

It's possible your jQuery code is running before the images have loaded, so the widths and heights calculated for the image may be coming out as 0. As the parent element is likely a block level element it will have a width, and this would explain why the calculation always ends up in the "else" section.

You could try wrapping the whole thing in $(window).load(function() {}); instead of document ready, this will wait until the whole page including images has loaded. http://api.jquery.com/load-event/

$(window).load(function() { // this will fire after the entire page is loaded, including images }); 

So....

$LSC(window).load(function(){ $LSC(".pg-cat-image").each(function(){ var real_width = $LSC(this).width(); var real_height = $LSC(this).height(); var parentwidth = $LSC(this).parent().width(); var parentheight = $LSC(this).parent().height(); var ratioParent = parentwidth / parentheight; var ratio = real_width / real_height; if (ratioParent < ratio) { $LSC(this).css({'width' : '100%', 'height' : 'auto'}); }else{ $LSC(this).css({'width' : 'auto', 'height' : '100%'}); } }); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.