如何重置 jquery 动画以重新开始?
我已经构建了一段不错的代码,一些动画和一些单击/悬停事件,其中只有一小行。我打算在多个 html 文档上使用它(这是一个游戏,你必须得到正确的答案并继续下一个问题),用全页滑块构建在另一个 html 中(我不想加载 DOM 多个次,毫无意义):未解决的问题是:如何重置代码而不实际重新加载它?如何让它清除迄今为止的一切并重新开始? 我是一个初学者,在其他片段上构建东西。我想这一定是一些非常简单和基本的事情,以至于没有人回答它......用动画来恢复之前所做的所有事情是不好的:太多的东西以及绑定和解除绑定。
I have built a nice piece of code, some animation and some click/hover events, quite a small row of them. I intend to use it on multiple html-documents (it is a game, you have to get right answer and proceed with next question), built together in another html with full-page-slider (I don't want to load DOM multiple times, makes no sense): the unsolved question is: how to reset the code without actually reloading it? how to make it to clear everything so far and start over?
I am a beginner, building stuff upon others snippets. I guess it must be something so easy and basic that nobody answered it... The stuff to animate back all what is done previously is no good: too much stuff and binding and unbinding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我有一个动画,每次代码运行时都会保持该位置。为了修复它,我使用了:
所以我的代码是这样的:
I had an animation that was keeping the position each time the code ran. To fix it I used:
so my code was something like:
对于 jQuery,您可以使用 .finish(),在此之前,要重置您需要 .stop() 方法的一些参数。
https://api.jquery.com/stop/
For jQuery you can use .finish(), and before that, to reset you need a few parameters to the .stop() method.
https://api.jquery.com/stop/
我这样做:(但不确定这是否完全正确)
I do it this way: (not sure if it's entirely right, though)
$(element).stop(),然后重新运行动画。
$(element).stop(), then re-run your animation.
没有简单的答案,因为这完全取决于您所做的事情、需要重置什么状态、需要重置哪些 UI 元素等等。我想您必须构建自己的重置方法,将所有内容设置为其初始状态,或者...您可以采取简单的路径并重新加载页面。
There is no easy answer since it all depends on what you've done, what state you need to reset, what UI elements need to be reset and so on. I guess you have to build your own reset method that sets all things to their initial state or... you could take the easy path and just reload the page.
谢谢各位耐心解答。两者都是正确的:
如果是一个动画,则 stop 方法有效。
如果有多个链接动画 - 抱歉,没有简单的方法。如果您想要一个简短但不那么优雅的解决方案,请重新加载。要正确地做到这一点 - 学习 javascript 来构建函数的逻辑链。仅使用 jQuery 无法长时间运行。
Thank you for patient answers. Both are right:
in case of one animation the stop method works.
in case of multiple chained animations - sorry, there is no easy way. Reload if you want a short but not so elegant solution. To do it properly - learn javascript to build the logical chain of functions. You can't run long with just jQuery.
尝试
.finish()
。示例(尝试向按钮发送垃圾邮件):
Try
.finish()
.Example (try spamming the button):