簡體   English   中英

如何使用相同的“按鈕”緊接着執行兩個不同的jQuery動畫?

[英]How can I execute two different jQuery animations right after each other with the same “button”?

當我按下按鈕時,我動畫我的.header_inner向下移動。 但是當我再次按下按鈕或向上移動動畫時,如何讓它回到原來的位置?

碼:

<script>
  $('.handler').click(function(){
    $('.header_inner').animate({
        top:"-=-28%",
    }, 300);
});     
</script>

當我再次按下按鈕時,我希望我的.header_inner再次回到.header_inner

您可以創建一個布爾變量,指示最后一次移動是向上還是向下:

var upwards = false,
    newTop;
$('.handler').click(function(){
    if (upwards) newTop = '28';
    else newTop = '-28';
    $('.header_inner').animate({
        top:"-=" + newTop + "%",
    }, 300);
    upwards = !upwards;
});

相同但沒有全局變量:

function toggleAnimate(){
    $('.header_inner').animate({
        top:"-=" + ((toggleAnimate.upwards)? '28':'-28') + "%",
    }, 300);
    toggleAnimate.upwards = !toggleAnimate.upwards;
}
toggleAnimate.upwards = false;
$('.handler').click(toggleAnimate);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM