开发者

jQuery conditional for opacity?

开发者 https://www.devze.com 2023-01-30 16:10 出处:网络
How do you test for whether a div has opacity = 0? I have tried: if (jQuery(\"#slideshow div#prev\").css({opacity: 0}))

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开发者_JS百科 when the opacity is already 1.0?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消