Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute two different jQuery animations right after each other with the same "button"?

When I press the button I animate my .header_inner to move downwards. But how do I make it to go back to its original position when I press the button again or to animate it moving upwards?

Code:

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

I want my .header_inner to animate its way back again when I press the button again.

like image 983
belmin Avatar asked Jan 22 '26 15:01

belmin


1 Answers

You can create a boolean variable that indicates whether the last movement was upwards or downwards:

var upwards = false,
    newTop;
$('.handler').click(function(){
    if (upwards) newTop = '28';
    else newTop = '-28';
    $('.header_inner').animate({
        top:"-=" + newTop + "%",
    }, 300);
    upwards = !upwards;
});
like image 172
LostMyGlasses Avatar answered Jan 24 '26 05:01

LostMyGlasses