只运行一次 jQuery 动画,然后执行其他操作,然后反转动画
我有一个带有一组 li
的菜单,当鼠标悬停在其上时,会以动画方式显示容器的高度,然后显示相关的子 .subnav
。
我遇到的问题有两个。
当我快速将鼠标悬停在
li
上时,容器的动画速度减慢为从那时起它再次开始动画。我希望能够说“仅在将鼠标悬停在任何li
上时为其设置动画一次”,然后在悬停在容器外时将其返回到原始位置或子导航。当
.subnav
内容显示并且我将鼠标悬停在另一个触发器 li 上时,动画似乎正在尝试再次在容器上运行,因此意味着任何操作都会有延迟应该发生在之后。
我尝试过使用 unbind()
和 bind()
但不成功,我也尝试过使用 :animated
但我似乎无法得到逻辑正确。
这是我的代码:
var navHeight = $('#primary-nav').height();
$('#primary-nav-list li').hover( function() {
var elem = $(this);
if ($(this).is('#roc-noir')){ var subnavHeight = 173; }
else { var subnavHeight = 235; }
$('#primary-nav').stop(true,false).animate({height:subnavHeight}, function(){
$('#primary-nav-list li').removeClass('active');
$(this).addClass('open');
$(elem).addClass('active');
$('#primary-nav-list li').not(this).find('.subnav').fadeOut('fast');
$(elem).find('.subnav').fadeIn('fast');
});
}, function(){
$('#primary-nav').removeClass('open');
$('#primary-nav').stop(true,false).animate({height:navHeight}, function(){
$('#primary-nav-list li').removeClass('active');
$('#primary-nav-list li').not(this).find('.subnav').hide();
});
});
任何帮助将不胜感激。
I have a menu with a set of li
's which, when hovered over, animates the height of the container and then shows the related child .subnav
.
The problems I am having are 2fold.
When I hover over the
li
's quickly, the animation of the container slows down as it is starting the animation again from that point. I would like to be able to say "Only animate this once when hovering over anyli
's" and then return it to it's original position when hovering out of the container or subnav.When the
.subnav
content is showing and I hover over another trigger li, the animation seems to be trying to run on the container again and as such means there is a delay in any actions that are supposed to occur after.
I have tried using unbind()
and bind()
but was unsuccessful, I also tried using :animated
but I can't seem to get the logic right.
Here is my code:
var navHeight = $('#primary-nav').height();
$('#primary-nav-list li').hover( function() {
var elem = $(this);
if ($(this).is('#roc-noir')){ var subnavHeight = 173; }
else { var subnavHeight = 235; }
$('#primary-nav').stop(true,false).animate({height:subnavHeight}, function(){
$('#primary-nav-list li').removeClass('active');
$(this).addClass('open');
$(elem).addClass('active');
$('#primary-nav-list li').not(this).find('.subnav').fadeOut('fast');
$(elem).find('.subnav').fadeIn('fast');
});
}, function(){
$('#primary-nav').removeClass('open');
$('#primary-nav').stop(true,false).animate({height:navHeight}, function(){
$('#primary-nav-list li').removeClass('active');
$('#primary-nav-list li').not(this).find('.subnav').hide();
});
});
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 Hover Intent 插件,看看它是否满足您的需要。它在开始之前添加了轻微的延迟,然后“停止”动画。
Take a look at the Hover Intent plugin and see if it does what you need. It adds a slight delay before starting, and subsequently "stopping" the animation.
也许是这样的——如果它已经在运行,则中止标志?
Maybe something like this -- a flag for abort if it is already running?