Home > Web Front-end > CSS Tutorial > How to Create a Circular Rotating Object Animation Using CSS and jQuery?

How to Create a Circular Rotating Object Animation Using CSS and jQuery?

Susan Sarandon
Release: 2024-12-11 04:21:18
Original
233 people have browsed it

How to Create a Circular Rotating Object Animation Using CSS and jQuery?

CSS for circular rotation of objects

Using CSS to rotate multiple objects around a circle is challenging. Here's how to best solve this problem:

Using jQuery

jQuery provides a simple solution to rotate any number of outer elements. The following is the code implemented using jQuery:

var radius = 100; // 调整以移动对象进出
var fields = $('.item'),
  container = $('#container'),
  width = container.width(),
  height = container.height();
var angle = 0,
  step = (2 * Math.PI) / fields.length;
fields.each(function() {
  var x = Math.round(width / 2 + radius * Math.cos(angle) - $(this).width() / 2);
  var y = Math.round(height / 2 + radius * Math.sin(angle) - $(this).height() / 2);
  if (window.console) {
    console.log($(this).text(), x, y);
  }
  $(this).css({
    left: x + 'px',
    top: y + 'px'
  });
  angle += step;
});
Copy after login

Using CSS animation

body {
  padding: 2em;
}

#container {
  width: 200px;
  height: 200px;
  margin: 10px auto;
  border: 1px solid #000;
  position: relative;
  border-radius: 50%;
  animation: spin 10s linear infinite;
}

.item {
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 50%;
  position: absolute;
  background: #f00;
  animation: spin 10s linear infinite reverse;
}

@keyframes spin {
  100% {
    transform: rotate(1turn);
  }
}
Copy after login
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
Copy after login

The above is the detailed content of How to Create a Circular Rotating Object Animation Using CSS and jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template