2011-01-30 56 views
0

IM有一個jQuery動畫功能動畫這是使用參數jquery的功能

$li = $("ol#update > li"); 

animate函數

function animate_li() 
{ 
    $li.filter(':first') 
     .animate({ 
     height: 'show', 
     opacity: 'show' 
     }, 250, function(){ 
     animate_li(); 
     }); 
    $li = $li.not(':first'); 
} 

指定列表現在我想參數,我也做了animate_li功能作爲:

function sp_animate_li(sp) 
{ 
    console.log ($li); 
    function animate_li() 
    { 
     $li.filter(':first') 
     .animate({ 
     height: 'show', 
     opacity: 'show' 
     }, sp, function(){ 
      animate_li(); 
     }); 
     $li = $li.not(':first'); 
    } 
} 

但現在我不能使用任何動畫

sp_animate_li(100); 

任何幫助

感謝

回答

1

你沒有調用內部函數,也沒有返回它,所以你實際上不能調用它。試試這個:

function sp_animate_li(sp) 
{ 
    console.log ($li); 
    function animate_li() 
    { 
     $li.filter(':first') 
     .animate({ 
     height: 'show', 
     opacity: 'show' 
     }, sp, function(){ 
      animate_li(); 
     }); 
     $li = $li.not(':first'); 
    } 
    return animate_li; 
} 

var inner = sp_animate_li(100); 
inner(); // calls the inner function 
+0

oops ... java和javascript是不同的... javascript可以映射變量功能...哇! – 2011-01-30 17:06:34

0

難怪,導致animate_li不被任何調用。爲什麼不將參數添加到animate_li而不是包裝它?

+0

oops ... java和javascript是不同的... – 2011-01-30 17:05:49

1

爲什麼不定義功能:

function animate_li($li, sp) 
{ 
    sp = sp || 250; 
    $li.first() 
     .animate({ 
      height: 'show', 
      opacity: 'show' 
      }, 
      sp, 
      function(){ 
       animate_li($li.not(':first'), sp); 
      } 
    ); 
} 

這也避免了使用從更高的範圍變量,可能會比較混亂......

更新:固定遞歸調用。

+0

這不會工作作爲animate_li是一個遞歸函數(被自己調用第9行),否則它將與原始只與sp參數... animate_li(sp) – 2011-01-30 18:54:01

0
function animate_li(sp) 
{ 
    sp = sp || 250; 
    $li.filter(':first') 
     .animate({ 
     height: 'show', 
     opacity: 'show' 
     }, sp, function(){ 
     animate_li(sp); 
     }); 
    $li = $li.not(':first'); 
}