JQuery:淡出,执行操作,然后淡入
所以我有一个相当于 html 表单的东西,但本身并不是一个实际的
从美学的角度来看,我希望表单淡出,在“消失”时重置,然后在完全重置时淡出。到目前为止,我已经有了这段代码来尝试实现此目的:
function Reset() {
$formDiv.fadeOut(function() {
// perform reset actions here
$(this).fadeIn()
});
}
但是,发生的情况是,当 div 淡出时,字段会被重置,因此用户会看到它们在淡出时物理上全部更改回默认值。然后,在淡出完成后,它会立即淡入。关闭,但我不希望用户看到字段被重置。我尝试了以下操作,所以它会等到 fadeOut 完成才能重置字段,但我遇到了无限循环或其他问题(浏览器说脚本运行缓慢并询问我是否要停止它):
function Reset() {
$formDiv.fadeOut(function() {
while (!$(this).is(':animated')) {
// perform reset actions here
}
$(this).fadeIn()
});
}
所以我不是真的很确定从这里去哪里。任何帮助将不胜感激。谢谢。
So I have what amounts to an html form, but is not an actual <form>
, per se. I have a 'Clear' button that I am currently using to reset all of the fields back to their defaults.
From an aesthetic standpoint, I would like for the form to fade out, reset while it's "gone", and fade back in completely reset. I've got this code so far to try to achieve this:
function Reset() {
$formDiv.fadeOut(function() {
// perform reset actions here
$(this).fadeIn()
});
}
However, what happens is, as the div is fading out, the fields get reset, so the user sees them all physically change back to their defaults while it's fading out. Then it fades back in as soon as it has finished fading out. Close, but I don't want the user to see the fields getting reset. I tried the following so it would wait until the fadeOut completed to reset the fields, but I got an infinite loop or something (the browser said the script was running slowly and asked me if I wanted to stop it):
function Reset() {
$formDiv.fadeOut(function() {
while (!$(this).is(':animated')) {
// perform reset actions here
}
$(this).fadeIn()
});
}
So I'm not really sure where to go from here. Any help would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最近在
fadeOut
上遇到了同样的问题,似乎工作不正确,我找到了解决方案:它看起来与 fadeIn/fadeOut 相同,但它按预期工作(在元素时进行更改)是看不见的)
我希望你们中的一些人会发现它很有用。
I had the same problem lately with
fadeOut
that seems to be working incorrectly, and I found the solution for it:It looks the same as fadeIn/fadeOut but it works as is expected (changes are made while element is invisible)
I hope some of you will find it useful.
用于
防止将淡出动画排队。
http://api.jquery.com/stop/
Use
to prevent queueing up the fadeOut Animation.
http://api.jquery.com/stop/
在动画完成之前,回调确实不应该触发。您是否尝试过在 fadeOut 上设置速度 - 文档(并不总是准确)将其显示为必需参数,如果您指定回调,则可能会这样 - 即,它可能会将函数评估为速度如果您没有明确提供。
编辑:查看代码后,看起来(至少在 1.3.2 中)如果您提供一个函数作为第一个参数,它将对其求值并使用返回值作为速度。如果您指定速度,那么您的回调函数应该按您的预期工作。该元素应该淡出,然后回调将触发,并且您的重置和淡入代码将被执行。
The callback really shouldn't fire until the animation has completed. Have you tried setting a speed on the fadeOut -- the documentation (which isn't always accurate) shows it as a required parameter and it just might be if you specify a callback -- i.e., it may be evaluating the function as the speed if you don't supply one explicitly.
EDIT: After looking through the code, it appears (in 1.3.2, at least) that if you supply a function as the first argument, it will evaluate it and use the return value as the speed. If you specify a speed, then your callback function it should work as you expect. The element should fade out, the callback will then fire and your reset and fade in code will be executed.
查看 .delay
更多详细信息:设置动画在 fadeOut() 中花费的时间,并对 fadeIn() 动画使用比 fadeOut 动画时间长的延迟。在此示例中,重置操作将在淡出动画期间发生,并且大概不会超过 600 毫秒。
编辑:哎呀,稍微误解了这个问题。您希望重置表单逻辑在 fadeIn() 调用上触发,而不是在 fadeOut 上触发。但我觉得同步动画事件还是不错的。尝试类似的方法:
Look into .delay
More detail: Set the amount of time the animation will take for fadeOut(), and use a delay for the fadeIn() animation that is longer than the fadeOut animation time. In this example, the reset actions will happen during the fadeOut animation and presumably not take longer than 600 ms.
Edit: Oops, misunderstood the problem slightly. You want the reset form logic to be trigger on the fadeIn() call, not the fadeOut. But I think it's still good to synchronize the animation events. Try something like: