简体   繁体   English

延迟slideDown()/ slideUp()-jQuery下拉列表

[英]Delay slideDown()/slideUp() - jquery dropdown

I am creating I am creating a drop down which I want to delay about 250 ms so that it's not triggered when someone quickly scrolls across the button. 我正在创建我正在创建的下拉菜单,我希望延迟约250毫秒,以便在有人快速滚动按钮时不会触发该下拉菜单。

Here's my current code. 这是我当前的代码。 I tried using the delay() method but it's not going well. 我尝试使用delay()方法,但效果不佳。

$(".deltaDrop").hover(function(){
    $('.deltaDrop ul').stop(false,true).slideDown(250);
    $('.delta').css('background-position','-61px -70px');
},function(){
    $('.deltaDrop ul').stop(false,true).slideUp(450);
    $('.delta').css('background-position','-61px 0');
});

Thanks 谢谢

var timer;
timer =     setTimeout(function () {
                    -- Your code goes here!
                }, 250);

Then you can use the clearTimeout() function like this. 然后,您可以像这样使用clearTimeout()函数。

 clearTimeout(timer);

This should work. 这应该工作。

$(".deltaDrop").hover(function(){
    $('.deltaDrop ul').stop(false,true).hide(1).delay(250).slideDown();
    $('.delta').css('background-position','-61px -70px');
},function(){
    $('.deltaDrop ul').stop(false,true).show(1).delay(450).slideUp();
    $('.delta').css('background-position','-61px 0');
});

.delay only works when you're dealing with the animation queue. .delay仅在处理动画队列时有效。 .hide() and .show() without arguments don't interact with the animation queue. 不带参数的.show() .hide().show()不会与动画队列交互。 By adding the .hide(1) and .show(1) before the .delay() makes the slide animations wait on the queue. 通过添加.hide(1).show(1)的前.delay()使得滑动动画等待队列中。

  setTimeout(function() {
    $('.deltaDrop ul').slideDown()
  }, 5000);

Untested, unrefactored: 未经测试,未经重构:

$(".deltaDrop")
  .hover(
    function()
    {
      var timeout = $(this).data('deltadrop-timeout');

      if(!timeout)
      {
        timeout =
          setTimeout(
            function()
            {
              $('.deltaDrop ul').stop(false,true).slideDown(250);
              $('.delta').css('background-position','-61px -70px');
              $('.deltaDrop').data('deltadrop-timeout', false);
            },
            250
          );
        $(this).data('deltadrop-timeout', timeout);
      }
    },
    function()
    {
      var timeout = $(this).data('deltadrop-timeout');
      if(!!timeout)
      {
        clearTimeout(timeout);
        $('.deltaDrop').data('deltadrop-timeout', false);
      }
      else 
      {
        $('.deltaDrop ul').stop(false,true).slideUp(450);
        $('.delta').css('background-position','-61px 0');
      }
    }
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM