带切换功能的 JQuery 淡入/淡出菜单
我对如何实现以下淡入/淡出菜单有点困惑。
我需要显示具有淡入/淡出效果的菜单项,但是无论我尝试什么,我都没有得到我想要的东西。示例:
用户单击菜单“汽车”。这将显示汽车链接在时尚中的淡出: a) 本田(淡入) b) 丰田(1秒后淡出) c) 三菱(2秒后淡出) d) Kia(3秒后淡出) ....
当用户单击另一个链接“摩托车”时,网站淡入菜单现在应该开始从最后一项(即起亚)淡出。
我需要在下一个项目仍在淡出/淡入时发生淡入/淡出行为。我需要达到这种渐进的效果。另外,我需要控制菜单项显示/隐藏的速度。
我尝试了递归,即
function showCarMenuItems(carsDiv){
var items = $(carsDiv).children();
displayMenu(items, 0);
};
function displayMenu(items, i){
var interval = 500;
$(items[i]).fadeIn(interval, function(){
if(i < items.length)
{
displayMenu(items, i++);
}
});
}
尽管它给了我一个接近的行为,但项目的显示并不像我需要的那样渐进。也就是说,一旦第一个汽车项目完全出现,那么下一个就会显示,等等。我需要一个汽车项目几乎显示出来,另一个应该已经开始显示(淡入)。
请帮忙。
谢谢, 游击队
I'm a little stuck here on how I can implement the following fade in/out menu.
I need to show menu items with fade in/out effect, however whatever I try I don't get what I'm looking for. Example:
User clicks on a menu "Cars". This will show cars links in a fade in fashion:
a) Honda (fade in)
b) Toyota (fade in 1 second later)
c) Mitsubishi (fade in 2 seconds later)
d) Kia (fade in 3 seconds later)
....When user clicks on another link "Motorcycles", the website fade in menu should now start fading out from the last item (i.e. Kia).
I need the fade in/out behaviour to happen for the next item while the previous is still fading out/in. I need to achieve this gradual effect. Also, I need to have control how fast the menu items are shown/hidden.
I tried recursion i.e.
function showCarMenuItems(carsDiv){
var items = $(carsDiv).children();
displayMenu(items, 0);
};
function displayMenu(items, i){
var interval = 500;
$(items[i]).fadeIn(interval, function(){
if(i < items.length)
{
displayMenu(items, i++);
}
});
}
Even though it gave me a close behaviour, the display of items were not gradual as I need. That is, once the first car item fully appeared then the next one showed etc. I need as one car item is almost shown the other one should have started showing already (fading in).
Please help.
Thanks,
partizan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下 jQuery 中的
delay
方法,应该就是您所需要的。delay
也许将代码更改为:
Try out the
delay
method in jQuery, should be what you need.delay
Perhaps change the code to something like:
当您将回调函数传递给
.fadeIn()
时,回调将在动画完成后执行。您需要对此采取一些技巧:http://jsfiddle.net/mattball/sRsTB/
编辑
为 @CnTwo 指出,您可以也可以使用
.delay()
(它绝对比我的原始版本更干净):http://jsfiddle.net/mattball/K6mRv/
When you pass a callback function to
.fadeIn()
, the callback executes after the animation is complete. You need to be a little trickier about it:http://jsfiddle.net/mattball/sRsTB/
Edit
As @CnTwo points out, you can also do this using
.delay()
(it's definitely cleaner than my original version):http://jsfiddle.net/mattball/K6mRv/
只需使用 jQuery
.delay()
函数即可。在下面的示例中,每个跨度的淡入需要 7 秒才能完成,每个淡入在其后续淡入之前 2 秒开始。
这意味着:
http://jsfiddle.net/krmby/m4LPE/
Just use jQuery
.delay()
function.In the example below every span's fade-in takes 7 seconds to complete and every fade-in starts 2 second before it's successor.
This means:
http://jsfiddle.net/krmby/m4LPE/