How can I get jQuery load() to complete before fadeOut/fadeIn?

前端 未结 2 1606
一个人的身影
一个人的身影 2021-01-20 23:26

I want to do an AJAX call via jQuery load() and only once it returns, then fadeOut the old content and fadeIn the new content. I want to old content to remain showing until the

2条回答
  •  耶瑟儿~
    2021-01-20 23:59

    Use callbacks to the control the order of the calls.

    var $data = $('#data');
    $data.fadeOut('slow', function() { 
        $data.load('/url/', function() { 
            $data.fadeIn('slow'); 
        }); 
    });
    

    (Note: I'm not 100% sure about if using var $data = ... and $data.doStuff() will actually work - if it does, it saves you from having to look up the div in the DOM tree every time, but if it doesn't, just remove the first line and use $('#data') everywhere...

提交回复
热议问题