這將始終失敗,因爲參數animate_property保持「animate_property」,因爲它包裹在一個對象中。如何解決這個問題呢?jQuery動畫靈活屬性
AnimateIt("height",100);
function AnimateIt(animate_property, val)
{
$(".selector").animate({animate_property:val},500);
}
這將始終失敗,因爲參數animate_property保持「animate_property」,因爲它包裹在一個對象中。如何解決這個問題呢?jQuery動畫靈活屬性
AnimateIt("height",100);
function AnimateIt(animate_property, val)
{
$(".selector").animate({animate_property:val},500);
}
創建您的對象並在調用animate()
方法之前應用該鍵和值。您可以使用括號標記設置對象鍵:
AnimateIt("height",100);
function AnimateIt(animate_property, val)
{
animObj = {};
animObj[animate_property] = val;
$(".selector").animate(animObj ,500);
}
您可以使用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);
}
呀很好。像魅力一樣工作。 – bergman
很高興有幫助(: – George