2013-11-26 76 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