我有一个名为direction的变量,它存储:left,right.
我需要根据该变量更改受影响的保证金.
现在我正在使用条件,是否可以用变量的输出替换"左" direction?
$("#map").animate({"margin-left": "+=50px"}, 500);
Run Code Online (Sandbox Code Playgroud)
不是直接的,但您可以在使用它之前创建一个对象并对其进行操作animate():
var direction = "left";
var property = {};
property[ "margin-"+direction ] = "+=50px";
$("#map").animate( property, 500);
Run Code Online (Sandbox Code Playgroud)