|
|

楼主 |
发表于 2023-7-15 12:07
|
显示全部楼层
代码
- <style>
- #mydiv {
- margin: 20px auto 0;
- position: relative;
- width: 740px;
- height: 460px;
- border: 1px solid gray;
- }
- #ball {
- position: absolute;
- width: 30px;
- height: 30px;
- border-radius: 50%;
- background: lightgreen;
- box-shadow: inset 0 0 18px 0 darkgreen;
- }
- </style>
- <div id="mydiv">
- <div id="ball"></div>
- </div>
- <script>
- let mx = mydiv.offsetWidth / 2,
- my = mydiv.offsetHeight / 2,
- bx = ball.offsetWidth / 2,
- by = ball.offsetHeight / 2
- a = 0
- canMove = true
- aniId = null;
- let circle = () => {
- a = a < 360 ? a + 1 : 0;
- let x1 = mx + (mx - bx) * Math.cos(a * Math.PI / 180) - bx,
- y1 = my + (my - by) * Math.sin(a * Math.PI / 180) - by;
- ball.style.cssText += `
- left: ${x1}px;
- top: ${y1}px;
- `;
- canMove ? aniId = requestAnimationFrame(circle) : cancelAnimationFrame(aniId);
- }
- circle();
- mydiv.onclick = () => {
- canMove = !canMove;
- if(canMove) aniId = requestAnimationFrame(circle);
- }
- </script>
复制代码
|
|