Toggle animation?

久未见 提交于 2019-12-21 21:35:58

问题


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?


回答1:


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');
  });



回答2:


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();                       
});


来源:https://stackoverflow.com/questions/794912/toggle-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!