超时 jQuery 效果
我试图让一个元素淡入,然后在 5000 毫秒内再次淡出。 我知道我可以做类似的事情:
setTimeout(function () { $(".notice").fadeOut(); }, 5000);
但这只会控制淡出,我会在回调中添加上述内容吗?
I am trying to have an element fade in, then in 5000 ms fade back out again. I know I can do something like:
setTimeout(function () { $(".notice").fadeOut(); }, 5000);
But that will only control the fade out, would I add the above on the callback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
更新:从 jQuery 1.4 开始,您可以使用
.delay( n )
方法。 http://api.jquery.com/delay/注意:< code>$.show() 和
$.hide()
默认情况下不会排队,因此如果您想将$.delay()
与它们一起使用,你需要这样配置它们:你可以使用 Queue 语法,这可能有效:
或者你可以非常巧妙地创建一个 jQuery 函数来完成它。
这将(理论上,在这里处理内存)允许你这样做:
Update: As of jQuery 1.4 you can use the
.delay( n )
method. http://api.jquery.com/delay/Note:
$.show()
and$.hide()
by default are not queued, so if you want to use$.delay()
with them, you need to configure them that way:You could possibly use the Queue syntax, this might work:
or you could be really ingenious and make a jQuery function to do it.
which would ( in theory , working on memory here ) permit you do to this:
我刚刚想通了:
我会为其他用户保留该帖子!
I just figured it out below:
I will keep the post for other users!
@strager 的伟大黑客。 像这样将其实现到 jQuery 中:
然后将其用作:
Great hack by @strager. Implement it into jQuery like this:
And then use it as:
你可以做这样的事情:
遗憾的是,你不能只做 .animate({}, 2000) ——我认为这是一个错误,并且会报告它。
You can do something like this:
Sadly, you can't just do .animate({}, 2000) -- I think this is a bug, and will report it.
Ben Alman 为 jQuery 编写了一个名为 doTimeout 的实用插件。 它有很多不错的功能!
在这里查看:jQuery doTimeout:类似于 setTimeout,但更好。
Ben Alman wrote a sweet plugin for jQuery called doTimeout. It has a lot of nice features!
Check it out here: jQuery doTimeout: Like setTimeout, but better.
为了能够像这样使用它,您需要返回
this
。 如果没有返回,fadeOut('slow') 将无法获得执行该操作的对象。即:
然后这样做:
To be able to use it like that, you need to return
this
. Without the return, fadeOut('slow'), will not get an object to perform that operation on.I.e.:
Then do this:
只需几行 jQuery 即可完成此操作:
请参阅下面的小提琴以获取工作示例...
http:// /jsfiddle.net/eNxuJ/78/
This can be done with only a few lines of jQuery:
see the fiddle below for a working example...
http://jsfiddle.net/eNxuJ/78/