欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > CSS+JQuery 实现弹力球效果,碰到屏幕边框弹回

CSS+JQuery 实现弹力球效果,碰到屏幕边框弹回

2025/11/10 8:54:38 来源:https://blog.csdn.net/weixin_38787928/article/details/143907307  浏览:    关键词:CSS+JQuery 实现弹力球效果,碰到屏幕边框弹回

实现弹力球效果,碰到屏幕边框弹回,效果如下

代码如下:

<img src="../image/ball.png" alt="" class="ball">
<style>.ball {position: fixed;top: 50vh;left: 50vw;width: 15vw;height: 15vw;border-radius: 50%;z-index: 0;}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script>const ball = document.querySelector('.ball');let posX = window.innerWidth / 2;let posY = window.innerHeight / 2;let velX = 2;let velY = 2;const friction = 0.5;let rotation = 0;const rotationSpeed = 3;function moveBall() {posX += velX;posY += velY;// Bounce off the edgesif (posX <= 0 || posX + ball.offsetWidth >= window.innerWidth) {velX = -velX * friction;posX = Math.min(Math.max(posX, 0), window.innerWidth - ball.offsetWidth);}if (posY <= 0 || posY + ball.offsetHeight >= window.innerHeight) {velY = -velY * friction;posY = Math.min(Math.max(posY, 0), window.innerHeight - ball.offsetHeight);}rotation += rotationSpeed;ball.style.left = posX + 'px';ball.style.top = posY + 'px';ball.style.transform = 'rotate(' + rotation + 'deg)';requestAnimationFrame(moveBall);}moveBall();
</script>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词