我正在实现一个有 8 个扇区的圆圈,每个扇区包含 8 个字符
如何在每个扇区固定位置显示8个字符,以便左右旋转时位置相同,唯一改变的是旋转的字符
我尝试计算每个扇区的坐标以显示 8 个字符,但我一直面临一些扇区在字符之间没有空格的问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Rotating Characters</title>
</head>
<body>
<div class="circle-container">
<div class="sector">
<div class="character">A</div>
<div class="character">B</div>
<div class="character">C</div>
<div class="character">D</div>
<div class="character">E</div>
<div class="character">F</div>
<div class="character">G</div>
<div class="character">H</div>
</div>
<!-- Repeat the sector div for each sector -->
<!-- ... -->
</div>
<script src="script.js"></script>
</body>
</html>
CSS(样式.css):
body {
margin: 0;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.circle-container {
position: relative;
width: 300px;
height: 300px;
}
.sector {
position: absolute;
width: 100%;
height: 100%;
clip-path: polygon(50% 50%, 0 0, 100% 0);
}
.character {
position: absolute;
width: 100%;
text-align: center;
transform-origin: 50% 100%;
transform: rotate(calc(var(--rotation, 0deg) + 45deg)); /* Initial rotation is 45 degrees */
}
JavaScript(script.js):
document.addEventListener('DOMContentLoaded', () => {
const sectors = document.querySelectorAll('.sector');
const characters = document.querySelectorAll('.character');
const totalSectors = sectors.length;
const totalCharacters = characters.length;
const degreesPerSector = 360 / totalSectors;
const degreesPerCharacter = 360 / totalCharacters;
characters.forEach((character, index) => {
const currentSectorIndex = Math.floor(index / (totalCharacters / totalSectors));
const rotation = degreesPerSector * currentSectorIndex;
character.style.setProperty('--rotation', `${rotation}deg`);
});
});
此示例生成一个具有八个扇区的圆形图案,每个扇区有八个字符。这些字母均匀地分散在其扇区内,并且可以通过更改 CSS --rotation 选项来旋转整个结构。每个字符的旋转是由 JavaScript 代码根据其在布局内的位置计算的。