Search code examples
javascriptjquerycss-transforms

Increment jquery rotate deg based off previous value


I seem to be finding conflicting results when searching online of how to do this. Using jquery(^3.4.1) all I want to do it increment the rotation of an element based off the previous rotation degrees. From what I have found I should be able to do:

$(element).css({'transform': 'rotate(+=10deg)'});

But no dice. However if I change it from rotate to left, it works as intended:

$(element).css({'left': '+=10'});

So what am I doing wrong? How do I increment the rotation like I do when I increment the left value?

Any advice?


Solution

  • jQuery will increment values only if they are numbers like in your second case, you need to store a variable to increment the rotation because its essentially a string.

    var rot = 0
    
    ...
    
    rot+=10
    $(element).css({'transform': `rotate(${rot}deg)`});