Dynamically choosing css property in animation
Posted
by paddywhack
on Stack Overflow
See other posts from Stack Overflow
or by paddywhack
Published on 2010-04-23T10:33:17Z
Indexed on
2010/04/23
10:43 UTC
Read the original article
Hit count: 138
jQuery
Hi,
It seems a straightforward thing but I'm not having much success. I'm just implementing a simple animation moving a div left or up using animate() but I would like to be able to set the "top" and "left" css properties dynamically. I would like to use the same function rather than have to have two, one for "left" and one for "top".
Here's some code which gives the idea.
function test($element){
$element.click(function(){
var cssProperty;
var direction = "left";
var moveTo = "100px";
if (direction === "top") {
cssProperty = "top";
} else {
cssProperty = "left";
}
/*Using variable as CSS property - This doesn't work */
$(this).animate({
cssProperty: moveTo
}, 1000);
/*Using variable as the CSS Values - This does */
$(this).animate({
left: moveTo
}, 1000);
});
}
Variables works on the css value side but not on the css selector side. Anyone have any suggestions?
Thanks
© Stack Overflow or respective owner