How to create circles around a circle with css, javascript?

前端 未结 5 1498
感情败类
感情败类 2020-12-14 18:16

I would like to create a circle (without any animation) which is surrounded by other circles, like this:

\"my

5条回答
  •  忘掉有多难
    2020-12-14 18:54

    No one addressed the javascript aspect of this question. Below is a complete (albeit quick and dirty) web page that will draw 6 perfectly spaced circles around a parent circle's center using html, css3, and javascript; it uses pure javascript so no need to reference a jquery library. You should be able to see how you could easily extract methods from the code to control the number of satellite circles, their distance from the center of the parent, parent and satellite radii, satellite offset, etc:

    var div = 360 / 6;
    var radius = 150;
    var parentdiv = document.getElementById('parentdiv');
    var offsetToParentCenter = parseInt(parentdiv.offsetWidth / 2); //assumes parent is square
    var offsetToChildCenter = 20;
    var totalOffset = offsetToParentCenter - offsetToChildCenter;
    for (var i = 1; i <= 6; ++i) {
      var childdiv = document.createElement('div');
      childdiv.className = 'div2';
      childdiv.style.position = 'absolute';
      var y = Math.sin((div * i) * (Math.PI / 180)) * radius;
      var x = Math.cos((div * i) * (Math.PI / 180)) * radius;
      childdiv.style.top = (y + totalOffset).toString() + "px";
      childdiv.style.left = (x + totalOffset).toString() + "px";
      parentdiv.appendChild(childdiv);
    }
    #parentdiv {
      position: relative;
      width: 150px;
      height: 150px;
      background-color: #ac5;
      border-radius: 150px;
      margin: 150px;
    }
    
    .div2 {
      position: absolute;
      width: 40px;
      height: 40px;
      background-color: #ac5;
      border-radius: 100px;
    }
    
    
    
    
      
    
    
    
      

提交回复
热议问题