1

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?

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
user3612986
  • 315
  • 2
  • 7
  • 18

2 Answers2

4

Check the complete answer now, I was not aware that you need it on click,

var el = document.getElementById("thing");
    var st = window.getComputedStyle(el, null);
    var tr = st.getPropertyValue("transform") ||
        "FAIL";

    var values = tr.split('(')[1].split(')')[0].split(',');
    var a = values[0];
    var b = values[1];
    var c = values[2];
    var d = values[3];

    var angle = Math.round(Math.atan2(b, a) * (180 / Math.PI));

    $('button').click(function(){
         angle += 10;
         $('#thing').css({'transform': `rotate(${angle}deg)`});
          $('p').text(('Rotate: ' + angle + 'deg'));
    });
    
#thing{display: inline-block; background-color: red; padding: 20px; margin-left: 50px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Rotate</button>
    <div id="thing" style="transform: rotate(45deg);">a</div>
    <p></p>
Atul Rajput
  • 4,073
  • 2
  • 12
  • 24
1

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)`});
Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43