Positioning divs in a circle using JavaScript

后端 未结 4 448
深忆病人
深忆病人 2020-11-29 00:22

I am trying to position 15 div elements evenly in a circle with a radius of 150px. I\'m using the following code, which seems to give an oddly ecce

4条回答
  •  臣服心动
    2020-11-29 00:31

    1. Set the position to "absolute". This will allow "top" and "left" to position the divs from (0, 0). Using "relative" will position the divs from where they would normally be laid out.

    2. Change the center point of your circle from (0, 0) to something else, like (250, 250).

      circleArray[i].posx = 250 + Math.round((150*Math.cos(i*(2*Math.PI/15)))) + 'px';
      circleArray[i].posy = 250 + Math.round((150*Math.sin(i*(2*Math.PI/15)))) + 'px';
      circleArray[i].style.position = "absolute";
      

提交回复
热议问题