开发者

Hide multiple elements with jQuery and get one callback

开发者 https://www.devze.com 2023-04-04 06:15 出处:网络
The question is pretty straightforward. If I select two or more elements with jQuery and, for example, use jQuery\'s fadeOut() function to hide them, the callback function is invoked twice (for each e

The question is pretty straightforward. If I select two or more elements with jQuery and, for example, use jQuery's fadeOut() function to hide them, the callback function is invoked twice (for each element). Is there a way to only receive one callback?

The code I am c开发者_StackOverflow社区urrently using to perform this task is pasted below.

$('#element-1, #element-2').fadeOut( 250, function() { /* Callback invoked twice. */ });

A similar question has been posted before (jQuery multiple animate() callback), but the solution seems quite complicated for what seems a simple problem.


You can use $.when [docs] (deferred objects):

$.when($('#element-1, #element-2').fadeOut(250)).then(function() {
    // do something
});

DEMO

This works with any animation afaik.

0

精彩评论

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