|
|

楼主 |
发表于 2023-7-13 13:08
|
显示全部楼层
二楼效果的代码
- <style>
- #mydiv {
- margin: 20px auto;
- width: 500px;
- height: 260px;
- border: 1px solid gray;
- position: relative;
- }
- #ball {
- position: absolute;
- left: 0;
- top: 0;
- width: 20px;
- height: 20px;
- border-radius: 50%;
- background: purple;
- transition: left 1.5s, top 1s;
- cursor: pointer;
- }
- </style>
- <div id="mydiv">
- <div id="ball"></div>
- </div>
- <script>
- let setPos = (pa,son) => {
- let x = Math.random() * (pa.offsetWidth - son.offsetWidth),
- y = Math.random() * (pa.offsetHeight - son.offsetHeight);
- return {x, y};
- };
- ball.onclick = () => {
- let pos = setPos(mydiv,ball);
- ball.style.cssText += `
- left: ${pos.x}px;
- top: ${pos.y}px;
- `;
- }
- </script>
复制代码
|
|