user39980
user39980

Reputation:

Toggle animation?

I have this code:

$('.couch-hide').click(function() {
    $(this).animate({right:0},1000, 'easeOutBounce');           
});

I am trying to get it to toggle, so on first click, pop it out, then click again, and place it back inside. Would i need to set a variable as a tracker? To tell what stage it is at?

Upvotes: 2

Views: 6256

Answers (2)

Vikram
Vikram

Reputation: 6877

Generally jquery toggle works pretty much in a standard way as given below, however i have not really tried it with animation stuff but this may work.

$('.couch-hide').click(function() {
    $(this).animate({right:0},1000, 'easeOutBounce').toggle();                       
});

Upvotes: 0

Shog9
Shog9

Reputation: 159698

Just save the original value somewhere, and remember to stop any in-progress animation before starting a new one. The animation routines will take care of the rest:

var panel = $('.couch-hide');
var originalPos = panel.css("right");
panel.toggle(function() {
    $(this).stop().animate({right:0},1000, 'easeOutBounce');
  },
  function() {
    $(this).stop().animate({right:originalPos},1000, 'easeOutBounce');
  });

Upvotes: 5

Related Questions