Why does the following work?
<something>.stop().animate(
{ 'top' : 10 }, 10
);
Whereas this doesn’t work:
var thetop = 'top';
<something>.stop().animate(
{ thetop : 10 }, 10
);
To make it even clearer: At the moment I’m not able to pass a CSS property to the animate function as a variable.
{ thetop : 10 }is a valid object literal. The code will create an object with a property namedthetopthat has a value of 10. Both the following are the same:In ES5 and earlier, you cannot use a variable as a property name inside an object literal. Your only option is to do the following:
ES6 defines ComputedPropertyName as part of the grammar for object literals, which allows you to write the code like this:
You can use this new syntax in the latest versions of each mainstream browser.