views:

135

answers:

2

I'd like to somehow get the target opacity (the final value it is being animated to) of an element that is fading.

For example...

$('body').fadeTo(0.4); // 0.4
$('body').fadeIn(); // 1
$('body').fadeOut(); // 0
$('body').animate({ opacity: 0.7 }); // 0.7

Is this doable in jQuery?

Update

For some background, see my answer here on Stack Overflow. I was trying to help out another user on SO and decided to ask this question that related to my answer.

+3  A: 

I don't think there would be. There is no attributes of an object that tell where it is going, only what it is at currently. Jquery is merely animating the CSS Properties.

Failing all else...

var destination = 0.4;
$('body').fadeTo(destination);
//O wow. Now we know what is fading to!
Pyronaut
It is animating the property with a final value set though.
alex
See [Brian McKenna's answer](http://stackoverflow.com/questions/2970395/in-jquery-can-you-get-the-target-opacity-of-an-element-that-is-fading/2971376#2971376)
alex
+3  A: 

Hi Alex!

jQuery uses step functions internally, you can overwrite the jQuery.fx.step.opacity function to read the passed jQuery.fx object:

var old = jQuery.fx.step.opacity;

jQuery.fx.step.opacity = function( fx ) {
    console.log(fx.elem);
    console.log(fx.end);

    return old(fx);
};

The opacity step function is called on every step of every opacity animation. You would probably want to filter the above based on fx.elem.

fx.end is the final value of the animation, fx.now is the current value and fx.start is the starting value. fx.unit is the unit of the values (in px, em, %, etc).

Brian McKenna
Brian, many thanks! :) I'm going to try and get this custom selector going now, I'll let you know how it all goes. May you have a great weekend!
alex
Got it working! http://stackoverflow.com/questions/2815439/jquery-selector-to-check-if-an-element-is-animating-to-hidden/2962650#2962650
alex