Get calculated opacity of an element using jQuery

Is there a way in jQuery to get the element's true opacity? Of course, you can get the opacity of an element with

$element.css('opacity')

but if, say, his parent defined an opacity of 0.5, then the actual opacity of the element is half that determined by its opacity. Does jQuery have a function for this? If not, is there an existing plugin?

If the plugin is missing, can I calculate it myself, iterating over the parents of the elements until I get to the topmost parent, multiplying the CSS opacity at every step? Or is there something that I do not take into account?

+3
source share
1 answer

, , , :

var opacity = 1;
$element.parents().andSelf()
        .each(function(){ opacity *= $(this).css('opacity') || 1 });

, , - ( ). , , .

: http://jsbin.com/okase/2

+5

Source: https://habr.com/ru/post/1751042/


All Articles