Toggle animation?

Toggle animation?

三月梨花 发布于 2021-11-25 字数 345 浏览 935 回复 2 原文

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?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2022-06-07 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();                       
});
锦爱 2022-06-07 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');
  });