|
|

楼主 |
发表于 2022-8-5 13:01
|
显示全部楼层
本帖最后由 马黑黑 于 2022-8-5 13:03 编辑
实现方式一:requestAnimationFrame
<style>
#papa {
margin: auto;
width: 1024px;
height: 660px;
box-shadow: 3px 3px 24px #000;
position: relative;
}
.box {
position: absolute;
width: 100px;
height: 100px;
background: green radial-gradient(at 35% 35%, lightgray, transparent);
box-shadow: inset -8px -8px 80px -8px tan;
border-radius: 50% 50% 25% 50%;
transform: rotate(45deg);
cursor: pointer;
}
.box::before {
position: absolute;
content: '';
width: 26px;
height: 6px;
border: 2px solid #663399;
border-radius: 0 0 50% 50% / 0 0 100% 100%;
border-top: none;
right: -20px;
bottom: 0;
}
</style>
<div id="papa">
<div class="box"></div>
</div>
<script>
let box = document.querySelector('.box');
let moveX = 0, moveY = 0, stepX = 1, stepY = 1.5, flag = true;
requestAnimationFrame(motion);
box.onclick = () => {
flag = !flag;
if(flag) requestAnimationFrame(motion);
}
function motion() {
if(!flag) return false;
moveX += stepX;
moveY += stepY;
box.style.left = moveX + 'px';
box.style.top = moveY + 'px';
requestAnimationFrame(motion);
if(moveX < 0 || moveX > papa.offsetWidth - box.offsetWidth) stepX = -stepX;
if(moveY < 0 || moveY > papa.offsetHeight - box.offsetHeight - 30) stepY = -stepY;
}
</script>
|
|