开发者

What's jQuery's css syntax for addition and subtraction on numerical values?

开发者 https://www.devze.com 2023-01-09 22:40 出处:网络
I\'m certain I\'ve seen this before. This is how i remember it $cnta.css({ top:\'+=50\' }); Needless to say, it doesn\'t work :-)

I'm certain I've seen this before. This is how i remember it

$cnta.css({ top:'+=50' });

Needless to say, it doesn't work :-)

I know i can parse the css value, manipulate the result and put it back into css, but that takes twice the code.


Thanks for ans开发者_运维问答wers!


That works for .animate():

$cnta.animate({ top:'+=50' }, 0);

For .css(), you can pass a function that will do the addition for you:

$cnta.css('top', function(i,current) { return parseInt(current) + 50; });

$cnta.css('top', function(i,current) { return (parseInt(current) + 50 || 50); });​

EDIT: Changed the animation from 1 to 0. As @Nick Craver noted, a 0 animation will work.

EDIT: Gave a better answer for the .css() version. If the top has not been given any value yet, then doing .parseInt(current) will return NaN. Updated to correct it.

0

精彩评论

暂无评论...
验证码 换一张
取 消