只运行一次 jQuery 动画,然后执行其他操作,然后反转动画

发布于 2024-11-07 01:22:30 字数 1432 浏览 0 评论 0原文

我有一个带有一组 li 的菜单,当鼠标悬停在其上时,会以动画方式显示容器的高度,然后显示相关的子 .subnav

我遇到的问题有两个。

  1. 当我快速将鼠标悬停在 li 上时,容器的动画速度减慢为从那时起它再次开始动画。我希望能够说“仅在将鼠标悬停在任何 li 上时为其设置动画一次”,然后在悬停在容器外时将其返回到原始位置或子导航。

  2. .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.

  1. 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 any li's" and then return it to it's original position when hovering out of the container or subnav.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-11-14 01:22:30

查看 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.

尛丟丟 2024-11-14 01:22:30

也许是这样的——如果它已经在运行,则中止标志?

var navHeight = $('#primary-nav').height();

var running = false;

var controller = {
     hoverIn : function() {
       if (running) return;
       running = true;

       var elem = $(this);
       var subnavHeight = 235;

       if ($(this).is('#roc-noir')) { subnavHeight = 173; }

       $('#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');
       });         
    },
    hoverOut : function(){
       if (running) return;

       $('#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();
          running = false;
       });

    }

$('#primary-nav-list li').hover(controller.hoverIn,controller.hoverOut);

Maybe something like this -- a flag for abort if it is already running?

var navHeight = $('#primary-nav').height();

var running = false;

var controller = {
     hoverIn : function() {
       if (running) return;
       running = true;

       var elem = $(this);
       var subnavHeight = 235;

       if ($(this).is('#roc-noir')) { subnavHeight = 173; }

       $('#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');
       });         
    },
    hoverOut : function(){
       if (running) return;

       $('#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();
          running = false;
       });

    }

$('#primary-nav-list li').hover(controller.hoverIn,controller.hoverOut);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文