2013-08-16 40 views
0

我有一个功能,我也需要它,只有它看起来非常混乱和臃肿,所以我想知道,是否有更好的方式来编码下面?优化jQuery回调

function careerFlyIn(){ 

    var distance = $('.row').offset().top, 
     $window = $(window); 
    var distance = distance - 200; 
    var flyInR = $('.path-description'); 
    var flyInL = $('.path-title'); 

    $window.scroll(function() { 
     if ($window.scrollTop() >= distance) { 
      $('.career-path .row:first-child').find(flyInL).animate({ 'left' : '0px' } ,400, 'easeOutBounce', function() { 
       $('.career-path .row:nth-child(2)').find(flyInL).animate({ 'left' : '0px' } ,400, 'easeOutBounce', function() { 
        $('.career-path .row:nth-child(3)').find(flyInL).animate({ 'left' : '0px' } ,400, 'easeOutBounce', function() { 
         $('.career-path .row:nth-child(4)').find(flyInL).animate({ 'left' : '0px' } ,400, 'easeOutBounce', function() { 
          $('.career-path .row:nth-child(5)').find(flyInL).animate({ 'left' : '0px' } ,400, 'easeOutBounce', function() { }); 
         }); 
        }); 
       });    
      }) 
      $('.career-path .row:first-child').find(flyInR).animate({ 'right' : '0px' } ,400, 'easeOutBounce', function() { 
       $('.career-path .row:nth-child(2)').find(flyInR).animate({ 'right' : '0px' } ,400, 'easeOutBounce', function() { 
        $('.career-path .row:nth-child(3)').find(flyInR).animate({ 'right' : '0px' } ,400, 'easeOutBounce', function() { 
         $('.career-path .row:nth-child(4)').find(flyInR).animate({ 'right' : '0px' } ,400, 'easeOutBounce', function() { 
          $('.career-path .row:nth-child(5)').find(flyInR).animate({ 'right' : '0px' } ,400, 'easeOutBounce', function() { }); 
         }); 
        }); 
       });    
      }) 
     } 
    }); 

}; 

回答

2

创建要在列表中动画并使用异步递归的项目列表。

function animate(elements, callback) 
{ 
    if (elements.length){ 
     elements.eq(0).find(flyInR).animate({ 'right' : '0px' }, 400, 'easeOutBounce'); 
     elements.eq(0).find(flyInL).animate({ 'left' : '0px' }, 400, 'easeOutBounce', function(){ 
      // Do the remainder of the list (after first item) 
      animate(elements.slice(1), callback); 
     }); 
    } 
    else { 
     // All done, call the final callback 
     callback(); 
    } 
} 

animate($('.career-path .row'), function() 
{ 
    // do something when all items have finished animating 
}); 

您可以将此模式应用于任何一组类似的异步操作。在这个例子中,左边和右边的动画并行运行,但只有一个触发下一个事件(在这种情况下是左边的)。

+0

为什么downvote?这对许多异步问题起作用并且是有用的技术。 –

1

这有帮助吗?

$window.scroll(function() { 
    if ($window.scrollTop() >= distance) { 

    $('.career-path .row').each(function(i) { 
     $(this).find(flyInL).delay(400*i) 
      .animate({ 'left' : '0px' } ,400, 'easeOutBounce'); 

     $(this).find(flyInR).delay(400*i) 
      .animate({ 'right' : '0px' } ,400, 'easeOutBounce'); 
    }); 
    } 
}); 

使用jQuery .delay()方法,Sets a timer to delay execution of subsequent items in the queue

+2

通过使用'.delay()'方法,动画会一个接一个地发生。 –

+0

是的。我撤回我的评论:) –

0

尝试

function animate($rows, idx, selector, animate) { 
    $rows.eq(idx).find(selector).animate(animate, 400, 'easeOutBounce', function() { 
     if (idx < $rows.length - 1) { 
      animate($rows, idx + 1, selector, animate) 
     } 
    }); 
} 

var $rows = $('.career-path .row'); 
animate($rows, 0, flyInL, { 
    'left' : '0px' 
}) 
animate($rows, 0, flyInR, { 
    'right' : '0px' 
}) 

注:未测试