|
|

楼主 |
发表于 2023-12-26 09:27
|
显示全部楼层
代码
- <style>
- #mydiv {
- margin: 50px auto 0;
- width: 240px;
- height: 240px;
- border-radius: 50%;
- background: none;
- position: relative;
- animation: rotate 10s infinite alternate linear;
- --deg1: -120deg;
- --deg2: 120deg;
- }
- #mydiv::before {
- content: '';
- position: absolute;
- left: -20px;
- top: -20px;
- right: -20px;
- bottom: -20px;
- border-radius: 50%;
- box-shadow: 4px 8px 28px gray, inset 0 0 160px lightgreen;
- animation: rotate 10s infinite alternate linear;
- --deg1: 120deg;
- --deg2: -120deg;
- }
- li-zi {
- position: absolute;
- width: 40px;
- height: 40px;
- border-radius: 50%;
- background: olive;
- animation: move 4s infinite alternate linear;
- }
- @keyframes move {
- from { transform: translate(var(--x1),var(--y1)); }
- to { transform: translate(var(--x2),var(--y2)); }
- }
- @keyframes rotate {
- from { transform: rotate(var(--deg1)); }
- to { transform: rotate(var(--deg2)); }
- }
- </style>
- <div id="mydiv">
- <li-zi></li-zi>
- </div>
-
- <script>
- let lz = document.querySelector('li-zi');
- let r = mydiv.offsetWidth / 2; /* 半径 */
- let angle = 45; /* 初始角度 */
- /* 计算初始角度下圆周点XY坐标 */
- let hudu1 = Math.PI / 180 * angle;
- let x1 = r + r * Math.cos(hudu1),
- y1 = r + r * Math.sin(hudu1);
- /* 计算对应点角度下圆周点XY坐标 */
- let hudu2 = Math.PI / 180 * (angle + 180); /* 对应点角度=初始角度+180 */
- let x2 = r + r * Math.cos(hudu2),
- y2 = r + r * Math.sin(hudu2);
- /* 将两个点的坐标值减去小球半径后赋值给CSS变量 */
- lz.style.cssText += `
- --x1: ${x1 - 20}px;
- --y1: ${y1 - 20}px;
- --x2: ${x2 - 20}px;
- --y2: ${y2 - 20}px;
- `;
- </script>
复制代码
|
|