Domanda

Sto usando il seguente codice per animare un div.

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

Al termine dell'animazione, vorrei rimuoverla. Come posso farlo?

È stato utile?

Soluzione

animate accetta altri 2 parametri, quindi puoi farlo:

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

Non testato.

EDIT: testato: ecco la pagina intera che ho usato, che si espande a 300px per rendere più ovvia la rimozione:

<!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>

Altri suggerimenti

JQuery animate (params, [duration], [easing], [callback]) offre la possibilità di aggiungere un callback che viene chiamato ogni volta che l'animazione viene completata

  

callback (Opzionale) Funzione: A   funzione da eseguire ogni volta che   l'animazione è completata, viene eseguita una volta per   ogni elemento animato contro.

La sintassi è praticamente semplice da jQuery:

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

    })
</script>

Vedi anche questo Quindi domanda

Metti la chiamata remove () nella coda degli effetti in questo modo:

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

Usa window.timeout con lo stesso tempo impiegato dall'animazione.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top