【问题标题】:How can I animate a div on a click, and then reverse the animation on a second click?如何在单击时为 div 设置动画,然后在第二次单击时反转动画?
【发布时间】:2013-05-29 23:33:23
【问题描述】:

我已经花了好几个小时尝试任何我能做的事情,但到目前为止我找不到任何答案。

我正在尝试创建一个动画,其中访问者单击一个方形图像,该图像将“翻转”为类似于新 Windows 徽标的梯形形状。同时,从该元素后面,另一个 div 将滑出到右侧,其中包含有关图像的信息。

创建这个很好,查看一个工作示例on my site, here:

(这目前仅适用于 webkit 浏览器,因为我还没有找到供应商前缀,并且后面的 div 还没有正确滑出,但我相信你可以理解我在这里想要实现的目标 -类似于 Windows 8 开始屏幕磁贴。)

使用的动画是来自 animate.css 的 FlipInY,其中中间的关键帧被移除。我原本打算使用 Hover 来运行动画,但是尝试在移动网站上实现它会很困难,因此 jQuery 'onClick' 事件似乎是一个更好的选择。

我已经设法让 onClick switchClass JQuery 函数工作,但仅适用于第一个动画,第二次单击只是再次启动动画,而不是真正反转它。我会在此处包含代码,但我将其删除以寻找我尚未找到的更好的解决方案。

This question and answer goes a way to solving the problem, however it is for a different 'slideUp' animation and uses two different buttons, which i'd rather replace with one button that would toggle the effect.

非常感谢任何帮助,如果您认为我以完全错误的方式尝试此操作,请提出其他建议!

【问题讨论】:

  • 嘿伙计,我喜欢你网站上的设计!
  • 非常感谢!还没有,但感谢您的赞赏!

标签: jquery html jquery-ui css


【解决方案1】:

你试过了吗:

animation-direction:alternate;
-webkit-animation-direction:alternate; /* Safari and Chrome */

BTW 效果非常好 :)

【讨论】:

  • 我已经尝试过了,但它不会将正方形“恢复”到其原始状态,如果您再次尝试该站点,我已经添加了此代码,但动画仅在我将鼠标悬停在框上时播放,当我移动鼠标时,它就会恢复到原来的形状。
【解决方案2】:

如果我是你,我会使用 jQuery toggleClass();

【讨论】:

  • 我确实尝试了toggleClass,它是添加/删除类和switchClass的组合,但是它不想反转动画,或者我做错了。简单的过渡(例如更改背景颜色)有效,但我试图实现的可能太复杂了,但我不明白为什么。
【解决方案3】:

这对你有用吗?

http://blog.guilhemmarty.com/flippy/jquery.flippy.min.js

$('#rectangle').click(function() {
  $(this).toggle(
      function(){
        $(this).flippy({
            color_target: "#aa0621",
            direction: "right",
            duration: "750"
         });
      },
      function(){
        $(this).flippy({
            color_target: "#aa0621",
            direction: "left",
            duration: "750"
         });
      }
  );
});

工作示例:http://jsfiddle.net/H6wA3/5/

【讨论】:

    【解决方案4】:

    也许我不明白你,但是:LIVE DEMO

    html:

    <div class="wrap">
    <div class="over">yellow</div>
    <div class="box">red</div>
    </div>
    

    js:

    $('.over').click(function() {
    $('.box').animate({marginLeft: '150',
                       width: '120',
                       height: '120',
                       marginTop: '-10'
                      }, 300);  
    
    
    setTimeout( function(){
        $('.box').css('zIndex',80);
    } , 400); 
    
    $('.box').animate({marginLeft: '0'}, 300).animate({width: '100', height: '100', marginTop: '0'}, 300);
    });
    
    $('.box').click(function() {
    $('.over').animate({
                        marginLeft: '150',
                        width: '120',
                        height: '120',
                        marginTop: '-10'
                        }, 300).css('zIndex',5); 
    
    $('.box').css('zIndex',10);
    setTimeout( function(){
        $('.over').css('zIndex',70);   
    } , 400); 
    $('.over').animate({
        marginLeft: '0'}, 300)
        .animate({width: '100',
              height: '100',
              marginTop: '0'}, 300);
    
    });
    

    【讨论】:

    • 这太棒了,非常感谢!我刚刚快速尝试在 Javascript 中添加一些其他动画,例如更改背景颜色,这可能吗?
    • '不确定我是否理解你:背景随着动画的变化?例如不透明度,红色平滑地变为黄色......
    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 2018-03-09
    • 2015-04-21
    • 2015-11-05
    • 2012-02-17
    相关资源
    最近更新 更多