2013-11-26 110 views
0

這將始終失敗,因爲參數animate_property保持「animate_property」,因爲它包裹在一個對象中。如何解決這個問題呢?jQuery動畫靈活屬性

AnimateIt("height",100); 

function AnimateIt(animate_property, val) 
{ 
    $(".selector").animate({animate_property:val},500); 
} 

回答

2

創建您的對象並在調用animate()方法之前應用該鍵和值。您可以使用括號標記設置對象鍵:

AnimateIt("height",100); 

function AnimateIt(animate_property, val) 
{ 
    animObj = {}; 
    animObj[animate_property] = val; 
    $(".selector").animate(animObj ,500); 
} 

JSfiddle

+0

呀很好。像魅力一樣工作。 – bergman

+0

很高興有幫助(: – George

0

您可以使用eval()

AnimateIt("height",100); 

function AnimateIt(animate_property, val){ 
    eval('$(".selector").animate({'+animate_property+':'+val+'},500)'); 
} 

或使用對象

AnimateIt("height",100); 

function AnimateIt(animate_property, val){ 
    var o={}; 
    o[animate_property] = val; 
    $(".selector").animate(o,500); 
} 
+1

更好地使用一個對象! – bergman

+0

是的,這是更好,我知道。 – Omid