Unless you are waiting for all the images to finish loading, then .height()
and .width()
will not return proper values as those values are only valid when the image has loaded. If they both return zero or undefined, then you will get the fullHeight class on all of them.
The solution here will be to use onload handlers and set the class when the image is loaded. Because images in the markup may load before your JS runs (particularly if they are in the browser cache), you will either have to check if the image is loaded and use it's height and width if loaded or, if not loaded yet, you will need to set an onload handler. Or, you can assign an onload handler in the markup with onload so that you're sure the load handler is installed before it loads.
Here's one way that checks to see if the image is loaded and adapts based on that:
$('.scale img').each(function(){
function setClass() {
var img = $(this);
if (img.height() < img.width()){
img.addClass('fullWidth');
} else {
img.addClass('fullHeight');
}
}
// if the image is loaded, then we can set the class now
if (this.complete) {
setClass.call(this);
} else {
// otherwise, must set class in load handler when it is loaded
this.onload = setClass;
}
});
If you can modify the markup, then you can do this which has the advantage of it's always set immediately as soon as the image loads:
Markup:
<ul class="slides">
<li class="scale"><img onload="scaleDynamic()" src="<?php echo $attachments->src('full'); ?>" /></li>
<li class="scale"><img onload="scaleDynamic()" src="<?php echo $attachments->src('full'); ?>" /></li>
<li class="scale"><img onload="scaleDynamic()" src="<?php echo $attachments->src('full'); ?>" /></li>
<li class="scale"><img onload="scaleDynamic()" src="<?php echo $attachments->src('full'); ?>" /></li>
<li class="scale"><img onload="scaleDynamic()" src="<?php echo $attachments->src('full'); ?>" /></li>
</ul>
Code:
// this must be a global function
function scaleDynamic() {
var img = $(this);
if (img.height() < img.width()){
img.addClass('fullWidth');
} else {
img.addClass('fullHeight');
}
}
FYI, if any of these images might be visible as they load, then you may want to set their default style to be visibility: hidden
and when they finish loading, set the style to visibility: visible
when you've set the scaling class and the image is loaded.