【问题标题】:Chaining Bouncing Entrances/ Exits with Animate.css使用 Animate.css 链接弹跳入口/出口
【发布时间】:2013-07-23 16:19:57
【问题描述】:

我刚开始玩 animate.css,尝试使用他们的 bounceInRight/ bounceOutLeft 动画。这个想法是有一个部分将bounceOutLeft,有它的容器slideUp()/下一个容器slideDown(),然后在下一个容器的内容上使用bounceInRight。

到目前为止,我的事件工作正常,但是当我应用我的bounceInRight 时,我的元素没有从最左边出现,它在正常位置。它只是有点动画。

示例时间! (请注意,这个回调纠缠的代码将被大量重构,我只是想让它先工作。)

$('#stat-switcher').on('click', '.graph-toggle', _publics.toggleGraph);

_publics.toggleGraph = function(e) {
  if (_viewMode != 'table' && _viewMode != 'graph') return false;

  var $table, $graph, nextView,
      animationEvents = 'animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd';

  if (_viewMode == 'table') {
    $table = $(this).closest('#stat-switcher').find('.table');
    $graph = $(this).closest('#stat-switcher').find('.graph');
    nextView = 'graph';
  } else {
    $table = $(this).closest('#stat-switcher').find('.graph');
    $graph = $(this).closest('#stat-switcher').find('.table');
    nextView = 'table';
  }

  _viewMode = 'transition';
  $(this).find('.icon').toggleClass('icon-bar-chart icon-table');

  $table.on(animationEvents, function() {
    $table.off(animationEvents);
    $table.slideUp(function() {
      $graph.slideDown(function() {
        $graph.on(animationEvents, function() {
          $graph.off(animationEvents);
          _viewMode = nextView;
        });
        $graph.toggleClass('bounceInRight bounceOutLeft');
      });
    });
  });

  $table.toggleClass('bounceInRight bounceOutLeft');
};

我认为我的问题的主要原因是我同时切换bounceInRightbounceOutLeft。也许有一种方法可以在我弄乱动画类之前确保我的元素离开页面?

【问题讨论】:

    标签: javascript css-animations animate.css


    【解决方案1】:

    现在您在 jQuery 中制作大部分动画,但您不必这样做。您可以在 animate.css 中制作这些动画。

    我现在正在解决同样的问题,所以这本身不是一个解决方案,但它是一个很好的方向。我创建了一个我希望发生的事件的时间线,然后我会随时触发这些事件。

    首先使用您要在 javascript 中添加的类创建元素:

    var animations = [
        {
            time:0,
            classes:"bounceInLeft"
            selector:"table"
        },
        {
            time:500,
            classes:"bounceInLeft"
            selector:"table"
        },
        {
            time:400,
            classes:"bounceInLeft"
            selector:"table"
        },
    ]
    

    现在在我们的 $(document).ready 中,我们将添加一些代码来遍历事件列表并创建一个时间线

    var timeline = 0;
    for(var i=0; i<animations.length; i++){
        timeline = parseInt(animations[i].time, 10) + parseInt(timeline, 10);
        runAnimation(i, timeline);
    }
    

    最后,我们需要函数“runAnimation”通过时间轴创建所有超时。

    function runAnimation(i, timeline){
        setTimeout(function(){
            console.log(i);
            $(animations[i].selector).addClass(animations[i].step);
        }, timeline);
    }
    

    现在您可以在对象数组中添加您想要的所有动画,其余的将由我们的另外两个 sn-ps 处理。

    玩得开心!

    【讨论】:

    • 不错!我也考虑过使用 setTimeouts 来解决这个问题,但我认为如果它运行缓慢,你会看到元素跳来跳去(我正在特别考虑将它们删除/添加到屏幕的动画。)我可能会使用这种方法而不是试图以这种方式捕捉事件和链式动画。
    猜你喜欢
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2013-11-26
    • 2018-11-08
    相关资源
    最近更新 更多