2013-08-16 42 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' 
}) 

注:未測試