html – 使用CSS在圆圈周围旋转对象?

前端之家收集整理的这篇文章主要介绍了html – 使用CSS在圆圈周围旋转对象?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图围绕一个圆圈旋转三个物体.到目前为止,我已经能够让一个物体围绕圆圈旋转.如果不弄乱代码,我无法获得多个.任何人都可以建议最好的方法来实现这一目标吗?这是代码和小提琴的一部分.谢谢!

这是Demo

  1. .outCircle {
  2. width: 200px;
  3. height: 200px;
  4. background-color: lightblue;
  5. left: 270px;
  6. position: absolute;
  7. top: 50px;
  8. -moz-border-radius: 100px;
  9. -webkit-border-radius: 100px;
  10. border-radius: 100px;
  11. }
  12. .rotate {
  13. width: 100%;
  14. height: 100%;
  15. -webkit-animation: circle 10s infinite linear;
  16. }
  17. .counterrotate {
  18. width: 50px;
  19. height: 50px;
  20. -webkit-animation: ccircle 10s infinite linear;
  21. }
  22. .inner {
  23. width: 100px;
  24. height: 100px;
  25. background: red;
  26. -moz-border-radius: 50px;
  27. -webkit-border-radius: 50px;
  28. border-radius: 100px;
  29. position: absolute;
  30. left: 0px;
  31. top: 0px;
  32. background-color: red;
  33. display: block;
  34. }
  35. @-webkit-keyframes circle {
  36. from {
  37. -webkit-transform: rotateZ(0deg)
  38. }
  39. to {
  40. -webkit-transform: rotateZ(360deg)
  41. }
  42. }
  43. @-webkit-keyframes ccircle {
  44. from {
  45. -webkit-transform: rotateZ(360deg)
  46. }
  47. to {
  48. -webkit-transform: rotateZ(0deg)
  49. }
  50. }
  1. <div class="outCircle">
  2. <div class="rotate">
  3. <div class="counterrotate">
  4. <div class="inner">hello
  5. </div>
  6. </div>
  7. </div>
  8. </div>

解决方法

Jquery解决方案适用于任何数量的外部项目.

Jquery从ThiefMaster♦无耻地偷走了他们在Q & A的回答

  1. var radius = 100; // adjust to move out items in and out
  2. var fields = $('.item'),container = $('#container'),width = container.width(),height = container.height();
  3. var angle = 0,step = (2 * Math.PI) / fields.length;
  4. fields.each(function() {
  5. var x = Math.round(width / 2 + radius * Math.cos(angle) - $(this).width() / 2);
  6. var y = Math.round(height / 2 + radius * Math.sin(angle) - $(this).height() / 2);
  7. if (window.console) {
  8. console.log($(this).text(),x,y);
  9. }
  10. $(this).css({
  11. left: x + 'px',top: y + 'px'
  12. });
  13. angle += step;
  14. });
  1. body {
  2. padding: 2em;
  3. }
  4. #container {
  5. width: 200px;
  6. height: 200px;
  7. margin: 10px auto;
  8. border: 1px solid #000;
  9. position: relative;
  10. border-radius: 50%;
  11. animation: spin 10s linear infinite;
  12. }
  13. .item {
  14. width: 30px;
  15. height: 30px;
  16. line-height: 30px;
  17. text-align: center;
  18. border-radius: 50%;
  19. position: absolute;
  20. background: #f00;
  21. animation: spin 10s linear infinite reverse;
  22. }
  23. @keyframes spin {
  24. 100% {
  25. transform: rotate(1turn);
  26. }
  27. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <div id="container">
  3. <div class="item">1</div>
  4. <div class="item">2</div>
  5. <div class="item">3</div>
  6. <div class="item">4</div>
  7. <div class="item">5</div>
  8. <div class="item">6</div>
  9. </div>

猜你在找的HTML相关文章