Eugenio Segala Eugenio Segala - 3 months ago 19
jQuery Question

if img < 500 apply css

i have this code:

var jq111 = jQuery.noConflict();

jq111('.slides img').each(function(){
jq111self = jq111(this).height();
if(jq111self < 500){
jq111(this).addClass( "product-rescue" );
}
});


if an image is less than 500 px apply a CSS class.

But there is a problem, is not working properly, and is applied 1/3 to images larger than 500px.

why?

https://jsfiddle.net/cuaqaxy8/3/

Answer

you need to wait for the image to load:

var jq111 = jQuery.noConflict();

jq111('.slides img').on("load", function() {
    var jq111self = jq111(this);
    if(jq111self.height() < 500){
        jq111self.addClass("product-rescue");
    }
});
Comments