简体   繁体   English

在jQuery中,你能获得一个褪色元素的“目标”不透明度吗?

[英]In jQuery, can you get the “target” opacity of an element that is fading?

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? 这在jQuery中可行吗?

Update 更新

For some background, see my answer here on Stack Overflow . 对于某些背景,请参阅Stack Overflow上的答案。 I was trying to help out another user on SO and decided to ask this question that related to my answer. 我试图帮助SO上的另一个用户,并决定问这个与我的答案有关的问题。

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. Jquery只是动画CSS属性。

Failing all else... 失败一切......

var destination = 0.4;
$('body').fadeTo(destination);
//O wow. Now we know what is fading to!

jQuery uses step functions internally, you can overwrite the jQuery.fx.step.opacity function to read the passed jQuery.fx object: jQuery在内部使用step函数,你可以覆盖jQuery.fx.step.opacity函数来读取传递的jQuery.fx对象:

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.elem过滤上述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.end是动画的最终值, fx.now是当前值, fx.start是起始值。 fx.unit is the unit of the values (in px, em, %, etc). fx.unit是值的单位(以px,em,%等表示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM