I’ve created an object literal of jQuery animations and I’m looking for a succinct way of passing this to animate().
Unfortunately, .apply() doesn’t seem to work with animate() so I’ve had to resort to using the indexes – I’m wondering why this is and how I can get it to work with .apply().
var animations = {
slideLeft : function (moveLeft) {
return [{
left: moveLeft
}, {
duration: 300,
easing: 'swingy'
}];
}
};
$randomEl.animate.apply(this, animations.slideLeft(100)); // doesn't work
$randomEl.animate(animations.slideLeft(100)[0], animations.slideLeft(100)[1]); // does work
You have to pass the right value for
thistoapply. I assume this works:If you call
animatenormally, like$randomEl.animate(...), then insideanimate,thiswill refer to$randomEl. Hence you have to pass$randomElas first argument toapplyand not whateverthisrefers to.See the MDN documentation for more information.