在单独启动的动画之间添加延迟
我有一个包含图像 URL 的数组,我将其作为 img
标签添加到页面中。我将它们隐藏起来,并在加载时淡入它们。这是可行的,但我想在它们之间添加一点延迟,这样就不会同时淡入太多。
function ajaxCallback(data)
{
if(data && data.images)
for(var n in data.images)
{
var image = $('<img>')
.prop(data.images[n])
.css({opacity: 0})
.appendTo('#output')
.load(imageLoaded);
}
}
function imageLoaded()
{
$(this)
.animate({opacity: 1});
}
目前,如果它们加载得足够快,它们基本上都会立即淡入。我想要每个之间有一点延迟。尝试添加对延迟的调用,但这似乎没有多大作用。我想我可能必须用队列或其他东西做一些事情,但不太明白如何做到这一点。
最好的方法是什么?
I have an array with image URLs I add to a page as img
tags. I add them hidden and fade them in when they are loaded. This works, but I'd like to add a bit of delay between each of them so it's not so much fading in at the same time.
function ajaxCallback(data)
{
if(data && data.images)
for(var n in data.images)
{
var image = $('<img>')
.prop(data.images[n])
.css({opacity: 0})
.appendTo('#output')
.load(imageLoaded);
}
}
function imageLoaded()
{
$(this)
.animate({opacity: 1});
}
At the moment, if they load quick enough, they will all basically fade in at once. I'd like a bit of delay between each. Tried adding a call to delay
, but that didn't seem to do much. Thinking I might have to do something with a queue or something, but can't quite get how to do this.
What's the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在类似的情况下(与图像无关),我使用递归函数提取数组的第一个元素,执行其工作,并在完成时调用自身。在您的情况下,您应该在
animate()
的回调中再次调用该函数。In a similar case (not related to images), I use a recursive function that extracts the first element of the array, does its work, and calls itself when it finish. In your case, you should call the function again in the callback of
animate()
.您可以在
imageLoaded()
方法中使用setTimeout
并为计时器提供一个随机值(在一定范围内)。You could use
setTimeout
in theimageLoaded()
method and give a random value (within some bounds) for the timer.这是一种方法:
示例: http://jsfiddle.net/redler/mBfpG/
Here's one way:
Example: http://jsfiddle.net/redler/mBfpG/