jQuery 如何延迟滑动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9197096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery How to slideUp with delay?
提问by TheBlackBenzKid
I am using the following jQuery. A div box slides up, and then after 5 seconds, fades out. Is there a way to achieve this as it takes a long time for the box to appear.
我正在使用以下 jQuery。一个 div 框向上滑动,然后在 5 秒后淡出。有没有办法实现这一点,因为框需要很长时间才能出现。
$(document).ready(function() {
$("#load_limit").slideUp(500); //have tried "fast" also
$("#load_limit").delay(5000);
$("#load_limit").slideDown(500);
});
回答by Samich
You can delay in the callback function:
您可以延迟回调函数:
$(document).ready(function() {
$("#load_limit").slideUp(500, function() {
$("#load_limit").delay(5000).slideDown(500);
});
});
or you can just simplified it:
或者你可以简化它:
$(document).ready(function() {
$("#load_limit").slideUp(500)
.delay(5000)
.slideDown(500);
});
回答by anonym
Find the div, wait for n seconds and then take n milliseconds transition time to slide up.
找到div,等待n秒,然后用n毫秒过渡时间向上滑动。
$("#div").delay(5000).slideUp(1000);
回答by Kato
What exactly is wrong with the code you have above? It looks functional (other than you have slideDown/slideUp and no fadeOut as you indicated in the instructions)
你上面的代码到底有什么问题?它看起来很实用(除了你有slideDown/slideUp而且没有你在说明中指出的淡出)
Here's an alternative way to achieve the same effect:
这是实现相同效果的另一种方法:
jQuery(function($) { // same as $(document).ready() but no conflicts :)
$('#load_limit').slideDown(500, function() {
var self = this;
setTimeout(function() {
$(self).fadeOut(500);
}, 5000);
});
});
回答by Uzbekjon
Reduce the time in your .slideUp()
to whatever you need. Here is an example:
将您的时间减少.slideUp()
到您需要的任何时间。下面是一个例子:
$("#load_limit").slideUp(50).delay(5000).slideDown(50);
$("#load_limit").slideUp(50).delay(5000).slideDown(50);
At 50ms you don't really see the .slideUp()
effect if your content's height is small. That's why it's better to use .hide()
instead.
.slideUp()
如果您的内容的高度很小,则在 50 毫秒时您不会真正看到效果。这就是为什么最好使用它.hide()
。