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>
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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With