fadeToggle jQuery callback before, not after

1.8k Views Asked by At

Is there a way to assign a callback before the fade animation occurs in fadeToggle? I am adjusting scroll positions and would like to adjust it before the object appears, not after.

3

There are 3 best solutions below

0
On BEST ANSWER

All you need to do is call your function, or run any code, directly after fadeToggle:

element.fadeToggle('fast');
element.scrollTop(300);

This will immediately set the scroll position, and the fade affect will follow.

Here is what I specifically had: visible and hidden are elements I just named as vars.

visible.fadeToggle('fast', function() {
    hidden.fadeToggle('fast');
    hidden.scrollTop(top_value);
});

When visible is hidden, the callback is invoked and the hidden element becomes visible. The scroll position is set immediately after, so you see the change before the fade has completed. This way you have a fluid fade and keep the scroll positions of both elements the same.

0
On

Call back gives you a way to perform something after the given action has completed. If you need to do something before operation (fading) just do it before calling the function itself.

0
On

would the fadeToggle override be an option for you ? if so then click the link below :)

Override jQuery functions