redconservatory
redconservatory

Reputation: 21924

jQuery conditional for opacity?

How do you test for whether a div has opacity = 0?

I have tried:

if (jQuery("#slideshow div#prev").css({opacity: 0})) 
    {
jQuery("#slideshow div#prev").animate({opacity: 1.0}, 500); 
    }

but it seems to fire off the animation even when the opacity is already 1.0?

Upvotes: 1

Views: 2190

Answers (2)

Jacob Relkin
Jacob Relkin

Reputation: 163258

Use css('opacity'):

if (!jQuery("#slideshow div#prev").css('opacity')) {
    jQuery("#slideshow div#prev").animate({opacity: 1.0}, 500); 
}

This code checks if the return value of .css('opacity') is falsy, if it is, then either the CSS hasn't been set or the value itself is falsy, in which case you would want to proceed and run the animate call.

Upvotes: 5

Tarun
Tarun

Reputation: 5514

Correct syntax would be

if (!jQuery("#slideshow div#prev").css('opacity')) 
    {
       jQuery("#slideshow div#prev").animate({opacity: 1.0}, 500); 
    }

css('opacity') would return 0 and if() condition will be become true.

Upvotes: 1

Related Questions