循环浏览导航栏的列表项并使用 jQuery 的 fadeIn() 动画淡入它们
我想做的是迭代导航的列表项,并按顺序淡入每个元素,但具有两个延迟选项的灵活性:等待每个先前动画完成后再继续下一个动画的选项,和/或每个动画开始之间的自定义延迟/超时(毫秒)(不是一个动画结束与下一个动画开始之间的延迟)。
当然,我可以使用嵌套回调并专门调用每个列表项,但这在代码方面相当低效,并且只会一个接一个地加载每个元素,这在某些情况下没问题,但在其他情况下我可能想要它们几乎同时加载,可能有轻微的延迟(以模拟我见过的一些基于 Flash 的导航)。这就是为什么我说自定义延迟选项应该在一个开始和下一个开始之间。
就其价值而言,导航元素首先通过 $('#nav li').hide();
隐藏,但我怀疑您已经猜到可能是这种情况。 :)
如何实现这种效果?
What I'd like to do is iterate through a navigation's list items, and fade each element in, sequentially, but with the flexibility of having two delay options: the option to wait for each previous animation to complete before proceeding with the next animation, and/or a custom delay/timeout (milliseconds) between the start of each animation (not a delay between the end of one animation and the start of the next animation).
Naturally, I could use nested callbacks and call each list item specifically, but that's rather inefficient code-wise, and would only load each element one-after-another, which is okay in some cases, but in others I might want to have them load nearly simultaneously, with a possible slight delay (to emulate some of the Flash based navigations I've seen). Hense why I say that the custom delay option should be between the start of one, and the start of the next.
For what it's worth, the navigation elements are first being hidden via $('#nav li').hide();
but I suspect you already guessed that might be the case. :)
How might this sort of effect be accomplished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查这个小提琴: http://jsfiddle.net/bPbw4/7/
假设你的导航 HTML 看起来像:
那么你的串行动画的JS代码可以是这样的:
在上面的代码中,
speed
是动画速度,gap
是每个fadeIn之间的延迟()
Check this fiddle: http://jsfiddle.net/bPbw4/7/
Suppose your nav HTML looks like:
Then your JS code for the serial animation can be something like this:
In the above code,
speed
is the animation speed andgap
is the delay between eachfadeIn()
我建议像这样的简单迭代
在这种情况下,我使用了延迟对象(jQuery 1.5+),但这种方法背后的想法是等待第 n 个项目的淡入淡出结束,然后迭代在第 n+1 个项目的
time
毫秒后运行。JsFiddle 示例: http://jsfiddle.net/McLZ4/3
I suggest a simple iteration like this
In this case I used deferred objects (jQuery 1.5+), but the idea behind this approach is to wait the end of the fadein on the
n
th item and then iterate the function aftertime
milliseconds on then+1
th item.a JsFiddle example : http://jsfiddle.net/McLZ4/3