|
|

楼主 |
发表于 2022-7-11 19:51
|
显示全部楼层
本帖最后由 马黑黑 于 2022-7-11 19:53 编辑
代码
- <style>
- #circle {
- margin: 50px auto 0;
- width: 400px;
- height: 400px;
- border-radius: 50%;
- background: radial-gradient(transparent,green);
- position: relative;
- }
- .ball {
- position: absolute;
- width: 20px;
- height: 20px;
- border-radius: 50%;
- background: linear-gradient(120deg,#eee,red);
- }
- </style>
- <div id="circle"></div>
- <script>
- let total = 20;
- let num = (min, max) => Math.floor(Math.random() * (max-min+1)) + min;
- for(j=0; j<total; j++) {
- let ele = document.createElement('span');
- ele.className = 'ball';
- let pos = calcCirclePos({x: 200, y: 200, r: num(0,180), a: num(0,360)});
- ele.style.left = pos.x1 - 10 + 'px';
- ele.style.top = pos.y1 - 10 + 'px';
- circle.appendChild(ele);
- }
- //求圆周点坐标函数: x、y 圆心坐标,r 半径,a 角度
- function calcCirclePos({x,y,r,a}) {
- let x1 = x + r * Math.cos((a * Math.PI) / 180);
- let y1 = y + r * Math.sin((a * Math.PI) / 180);
- return {x1,y1};
- }
- </script>
复制代码
|
|