Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating a div how to set around which point to rotate?

My javascript is:

$(function() {
    var $elie = $(".circle");
    rotate(0);
    function rotate(degree) {        
        $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});  
        $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});                      
        timer = setTimeout(function() {
            rotate(++degree);
        },5);
    }
}); 

How do I rotate the div around another center point?

Here's demo:

$(function() {
    var $elie = $(".circle");
    rotate(0);
    function rotate(degree) {        
        $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});  
        $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});                      
        timer = setTimeout(function() {
            rotate(++degree);
        },5);
    }
});
.circle {
  width: 200px;
  height: 200px;
  background: green;
  border-radius: 50%;
  border: 4px dashed black;
 
  margin: calc(50vh - 100px) auto 0 auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="circle">

</div>
like image 308
Ivanka Todorova Avatar asked Mar 04 '13 15:03

Ivanka Todorova


People also ask

How do I rotate an image with a point in CSS?

You could use transform-origin. It defines the point to rotate around from the upper left corner of the element. This would rotate around the upper left corner.

How do I rotate a Div 90 degrees?

We can add the following to a particular tag in CSS: -webkit-transform: rotate(90deg); -moz-transform: rotate(90deg); -o-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg);


1 Answers

You'd use transform-origin for that :

$elie.css("-webkit-transform-origin", "50% 100%" );

or

transform-origin: 100% 40%;
-ms-transform-origin: 100% 40%; /* IE 9 */
-webkit-transform-origin: 100% 40%; /* Safari and Chrome */
-moz-transform-origin: 100% 40%; /* Firefox */
-o-transform-origin: 100% 40%; /* Opera */

FIDDLE


EDIT:

To rotate around the center of the .circle element, it first needs a height, otherwise there is no center. Then you could use percentages, or the much handier center property, like so:

transform-origin:center center;

DEMONSTRATION

like image 60
adeneo Avatar answered Sep 28 '22 07:09

adeneo