当 css "left" 参数是百分比时,Velocityjs 给出语法错误

Velocityjs giving syntax error when css "left" parameter is a percentage

这个有效:

Velocity(document.getElementsByClassName('center')[0], { left:50 }, { duration: 1000 });

这不是:

Velocity(document.getElementsByClassName('center')[0], { left:50% }, { duration: 1000 });

有了这个我得到 Uncaught SyntaxError: Unexpected token }

我需要为此设置动画,使 css left 属性 为 50%。我该怎么做?

没关系我想出来了。

解决方案是将“50%”放在引号中

您需要 50% 作为一个字符串,因此您应该用 { left:'50%' }{ left:"50%" }.

等引号引起来

完整示例:

Velocity(document.getElementsByClassName('center')[0], { left:'50%' }, { duration: 1000 });