当前位置: 首页 > news >正文

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>

http://www.lryc.cn/news/488451.html

相关文章:

  • shell编程规范和脚本变量
  • jspm美容院管理系统
  • Prometheus结合K8s(二)使用
  • 【虚幻引擎】UE5数字人开发实战教程
  • 深入分析:固定参考框架在RViz中的作用与对数据可视化的影响 ros ubuntu20.04
  • Android:时间选择器(最下面有效果图)
  • 第十六届蓝桥杯模拟赛(第一期)-c++/c
  • 如何挑选路由器?需要看哪些参数?
  • mysql-备份(二)
  • Tailwind CSS 和 UnoCSS简单比较
  • unity3d————范围检测
  • 修改this.$confirm的按钮位置、图标、文字及标题
  • SQL MID() 函数详解
  • 【蓝桥杯备赛】123(前缀和的复杂应用)
  • MINES
  • H.265流媒体播放器EasyPlayer.js H5流媒体播放器关于如何查看手机端的日志信息并保存下来
  • uni-app快速入门(十一)--常用JS API(上)
  • Flink任务提交到yarn上slot数量为0的问题
  • vue3怎么根据字符串获取组件实例
  • ISUP协议视频平台EasyCVR私有化视频平台新能源汽车充电停车管理方案的创新与实践
  • 智领未来: 宏集物联网HMI驱动食品与包装行业迈向智能化新高度
  • redis-击穿、穿透、雪崩
  • 【Redis】服务器异常重启,导致redis启动失败
  • Springboot+Vue的项目搭建(三)
  • 【Word】一键批量引用论文上标——将正文字体改为上标格式
  • DAY1 网络编程(TCP客户端服务器)
  • 如何在Ubuntu当中利用CloudCompare软件进行点云配准拼接?
  • AWTK 最新动态:支持鸿蒙系统(HarmonyOS Next)
  • vue数据变化但页面不变
  • Leetcode128. 最长连续序列(HOT100)