Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an element AFTER animation completes?

I'm using the following code to animate a div.

<script>
  $(function() {
    $("a.shift").click(function() {
      $("#introOverlay").animate({
        height: 0,
      }, 2000)
    });
  });
</script>

When the animation finishes, I would like to remove it. How can I do that?

like image 463
Martin Avatar asked Sep 16 '09 12:09

Martin


2 Answers

animate takes 2 more params, so you could do:

$("a.shift")
    .click(function() 
        {
            $("#introOverlay")
                .animate({height: 0}, 2000,"linear",function()
                    {
                        $(this).remove();
                    }
                )
        }
    );

Untested.

EDIT: Tested: here's the full page I used, which expands to 300px make removal more obvious:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            //<![CDATA[
            $(document).ready(function()
            {
                $(".shift").click(function() 
                {
                    $("#introOverlay")
                    .animate({height: 300}, 2000,"linear",function()
                    {
                        $(this).remove();
                    })
                });
            });
            //]]>
        </script>
    </head>
    <body>
    <a class="shift" href="javascript:void(0)">clickme</a>
    <div id="introOverlay" style="background-color:red;height:200px;">overlay</div>
    </body>
</html>
like image 137
spender Avatar answered Nov 15 '22 11:11

spender


Put the remove() call in the effects queue like this:

$("a.shift").click(function() {
  $("#introOverlay").animate({
    height: 0,
  }, 2000);
  $("#introOverlay").queue(function() {
    $(this).remove();
    $(this).dequeue();
  });
});
like image 22
André Staltz Avatar answered Nov 15 '22 12:11

André Staltz