我似乎无法识别的神秘错误
我只是想练习 javascript 和 Jquery,所以我正在编写这个小脚本,它是对文档侧面的球弹跳的基本模拟。我觉得这应该是非常微不足道的,但我遇到了很多意想不到的行为。我怀疑这是由于缺乏对 DOM 等的理解。
我不会尝试详细解释有问题的行为,而是为您提供脚本的 jsfiddle。
正如你所看到的,当对象到达在页面顶部,它只是继续前进,告诉我处理 Y 坐标的条件正在评估 false,而实际上它应该是 true。
感谢您的帮助!
I am just trying to practice javascript and Jquery, so I'm working on this little script that is a rudimentary simulation of a ball bouncing of the sides of the document. This should be very trivial I feel, but I am running into a lot of unexpected behavior. This I suspect is due to a lack of understanding of DOM and such.
Rather than trying to explain in detail the problematic behavior, I'll provide you this jsfiddle of the script.
As you can see, when the object reaches the top of the page, it simply continues going, telling me that the conditional dealing with the Y coordinate is evaluating false, when it should be true.
Thanks for all the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您对位置进行动画处理的时间超过 10 毫秒...但您的循环执行速度比这更快,因此位置变化的速度不足以使循环中的比较有效。
The issue is that you are animating the position over 10ms... but your loop is executing faster than that, so the position isn't changing fast enough for the comparison in the loop to be valid.