如何使用javascript / jquery旋转由点组成的半圆

问题描述 投票:3回答:3

我有这个代码来创建一个小圆圈的半圆,但我不知道如何旋转它,以便半圆开始在圆的顶部而不是侧面(向左旋转45º)。

var items = 9;

for (var i = 0; i < items; i++) {

  var x = 200 + 50 * Math.cos(1.1 * Math.PI * i / items);
  var y = 100 + 50 * Math.sin(1.1 * Math.PI * i / items);

  $("body").append("<div class='point' style='left:" + x + "px;top:" + y + "px'></div>");

}
.point {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: white;
  border: 1px solid black;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Current OutputDesired Output

谢谢!

javascript jquery html css
3个回答
2
投票

我相信这就是你想要的:

var items = 8;

for(var i = 0; i < items; i++) {

    var x = 200 + 50 * Math.cos(Math.PI * i / items);
    var y = 100 + 50 * Math.sin(Math.PI * i / items); 
    
    $("#center").append("<div class='point' style='left:"+ x +"px;top:"+ y +"px'></div>");

}
#location {
  
    width:200px;
    height:200px;
    /* border-radius:100px; */
    background: black;
    position:relative;
    left:200px;
    top:100px;
    
}

#center {
  transform: rotateZ(-80deg);
}

.point {
  
    width:8px;
    height:8px;
    border-radius:50%;
    background: white;
    border: 1px solid black;
    position:absolute;
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="center"></div>

1
投票

只需反转顶部/左侧的x / y值:

var items = 9;
for (var i = 0; i < items; i++) {

  var x = 100 + 50 * Math.cos(1.1 * Math.PI * i / items);
  var y = 200 + 50 * Math.sin(1.1 * Math.PI * i / items);

  $("body").append("<div class='point' style='left:" + y + "px;top:" + x + "px'></div>");

}
.point {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: white;
  border: 1px solid black;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0
投票

你可以交换sincos电话。与您的问题无关,但也许您更喜欢添加元素的jQuery样式:

var items = 9;
for (var i = 0; i < items; i++) {
  var x = 200 + 50 * Math.sin(1.1 * Math.PI * i / items);
  var y = 100 + 50 * Math.cos(1.1 * Math.PI * i / items);
  $("body").append($("<div>").addClass("point").css({left: x, top: y}));
}
.point {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: white;
  border: 1px solid black;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.