gpt4 book ai didi

javascript - 以适当的 Angular 将div分布在一个圆圈中

转载 作者:行者123 更新时间:2023-11-29 18:57:39 26 4
gpt4 key购买 nike

我有一圈 div。它们是用 javascript 创建的,并均匀分布在圆圈周围。 div 的数量取决于我提供的一些数组。一个圆圈内可能有任意数量的 div。

这些 div 目前是水平的。

enter image description here

但是我想根据它们在圆圈中的位置以适当的 Angular 旋转每个。所以它看起来像这样:

enter image description here

不要介意第二张图的内容,应该是一样的:一、二、三等

有人可以帮我做这个吗?

所以这是我现在拥有的代码:

var content = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen'];


var container = document.getElementById("container");
var size = window.innerWidth > window.innerHeight ? window.innerHeight : window.innerWidth;
container.style.height = size + 'px';
container.style.width = size + 'px';

function createFields() {
for(var i = 0; i < content.length; i++) {
var div = document.createElement('div');
div.className = "field";
div.innerHTML = content[i];
container.appendChild(div);
}
}

function distributeFields() {
var myNodeList = document.querySelectorAll('.field');
var fields = Array.from(myNodeList);
var width = size/2;
var height = size/2;
var angle = 0;
var step = 2 * Math.PI / fields.length;
var radius = 0.7 * width;

var angle = -90 * Math.PI / 180;

fields.forEach(function(element) {
var x = Math.round(width + radius * Math.cos(angle));
var y = Math.round(height + radius * Math.sin(angle));
element.style.right = x + 'px';
element.style.top = y + 'px';
angle = angle - step;
});
}

window.onload = function() {
createFields();
distributeFields();
};
html, body { height: 100%; margin: 0; border:0; }
#container { background-color: DarkSeaGreen; box-sizing: border-box; position: relative; margin: auto; overflow: hidden !important; }
.field { width: 60px; height: 20px; position: absolute; color: #fff; background-color:indigo;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Circle</title>
</head>
<body>
<div id="container"></div>
</body>
</html>

最佳答案

您可以在 forEachfields.length 中使用 index 来获得 360 度的分数,或一个完整的圆。

var rotationBase = -90;
var rotation = rotationBase + 360 / fields.length * index;
element.style.transform = 'rotate(' + rotation + 'deg)';

完整的工作示例

var content = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen'];


var container = document.getElementById("container");
var size = window.innerWidth > window.innerHeight ? window.innerHeight : window.innerWidth;
container.style.height = size + 'px';
container.style.width = size + 'px';

function createFields() {
for (var i = 0; i < content.length; i++) {
var div = document.createElement('div');
div.className = "field";
div.innerHTML = content[i];
container.appendChild(div);
}
}

function distributeFields() {
var myNodeList = document.querySelectorAll('.field');
var fields = Array.from(myNodeList);
var width = size / 2;
var height = size / 2;
var angle = 0;
var step = 2 * Math.PI / fields.length;
var radius = 0.7 * width;

var angle = -90 * Math.PI / 180;

fields.forEach(function (element, index) {
var x = Math.round(width + radius * Math.cos(angle));
var y = Math.round(height + radius * Math.sin(angle));
element.style.right = x + 'px';
element.style.top = y + 'px';

var rotationBase = -90;
var rotation = rotationBase + 360 / fields.length * index;
element.style.transform = 'rotate(' + rotation + 'deg)';

angle = angle - step;
});
}

window.onload = function () {
createFields();
distributeFields();
};
html, body { height: 100%; margin: 0; border:0; }
#container { background-color: DarkSeaGreen; box-sizing: border-box; position: relative; margin: auto; overflow: hidden !important; }
.field { width: 60px; height: 20px; position: absolute; color: #fff; background-color:indigo;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Circle</title>
</head>
<body>
<div id="container"></div>
</body>
</html>

关于javascript - 以适当的 Angular 将div分布在一个圆圈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48610101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com