Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First you need to set the origin of rotation to top-left and the rotate,</p> <pre><code>$("#child").css({ "-webkit-transform-origin": "top left", "-moz-transform-origin": "top left", "-o-transform-origin": "top left", "transform-origin": "top left", "transform" : "rotate("+ angle +"deg)" }); </code></pre> <p>This will rotate the element around its (top, left) point.</p> <p>But as you described in your case, you need the div to be always in top,left you need to do some math with bounds of the div.</p> <p>you can get the bounds using <code>document.getElementById(ele).getBoundingClientRect();</code></p> <p>Then you need to get the bounds of the div before and after it was rotated. Apply the math logic based on the current angle. Find the code here for your reference.</p> <pre><code>$(function(){ $("#child").draggable(); var angle = 0; $("#btn1").click(function(){ angle = angle + 90; if(angle == 360) { angle = 0 } // reset top left position to (0,0) $("#child").css({"top": "0px", "left": "0px"}); var preBounds = getBounds("child"); $("#child").css({ "-webkit-transform-origin": "top left", "-moz-transform-origin": "top left", "-o-transform-origin": "top left", "transform-origin": "top left", "transform" : "rotate("+ angle +"deg)" }); var currBounds = getBounds("child"); if(angle == 90) { $("#child").css({ "left": currBounds.left+Number(preBounds.left-currBounds.left)+"px"}); } else if(angle == 180) { $("#child").css({ "top": currBounds.top+Number(preBounds.top-currBounds.top)+"px","left":currBounds.width+"px"}); } else if(angle == 270) { $("#child").css({ "top": currBounds.height+"px"}); } }); }); function getBounds(ele){ var bounds = document.getElementById(ele).getBoundingClientRect(); console.log(bounds); //{left:bounds.left, top:bounds.top, right:bounds.right, bottom:bounds.bottom} return bounds; } </code></pre> <p>Check the <a href="http://jsfiddle.net/sesubash/uhxPM/2/" rel="nofollow">fiddle</a> of the same. Hope it helps</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload