質問

次のコードを使用してdivをアニメーション化します。

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

アニメーションが終了したら、削除したいと思います。どうすればいいですか?

役に立ちましたか?

解決

animate はさらに2つのパラメーターを必要とするため、次のようにすることができます。

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

未テスト。

編集:テスト済み:使用したページ全体を以下に示します。300pxに拡張すると、削除がより明確になります。

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

他のヒント

JQuery animate(params、[duration]、[easing]、[callback])アニメーションが完了するたびに呼び出されるコールバックを追加する可能性を提供します

  

callback(オプション)機能:A   常に実行される関数   アニメーションが完了し、   各要素に対してアニメーション化。

構文はほとんどjQueryに簡単です:

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

    })
</script>

こちらもご覧ください質問

次のように remove()呼び出しをエフェクトキューに入れます:

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

window.timeoutは、アニメーションにかかる時間と同じ時間で使用します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top