jQuery悬停隐藏的内容

时间:2015-11-27 13:24:41

标签: jquery hover timeout mouseover

我有以下代码......

jQuery(function($) {
  var timer;

  $('.action-viewer').hide();
  $('.task').on('mouseover', function() {
    li = $(this);

    timer = setTimeout(function() {
      li.find('.action-viewer').show();
    }, 400);

  }).on('mouseout', function() {
    clearTimeout(timer);
    $(this).find('.action-viewer').hide();
  });
});
body {
  padding: 15px
}
* {
  padding: 0;
  margin: 0;
}
.task-list {
  list-style: none
}
.task {
  padding: 10px;
  border-top: 1px solid #EFEFEF
}
.action-viewer {
  border-top: 1px solid #CCC
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="task-list">
  <li class="task">This is task number 01
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <li class="task">This is task number 02
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <li class="task">This is task number 03
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <li class="task">This is task number 04
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <li class="task">This is task number 05
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <ul>

应该发生的事情是当您将鼠标悬停在任务上时,操作面板将显示,用户可以单击该链接。然而,当我将鼠标移向链接时,它会消失。这没有计时器,但我需要延迟。

有什么建议吗?

2 个答案:

答案 0 :(得分:4)

您可以使用mouseleave even t而不是mouseout event

这样做时,在离开元素并将鼠标悬停在孩子身上时不会触发事件。

以下是更新的示例:

jQuery(function($) {
  var timer;
  $('.action-viewer').hide();
  $('.task').on('mouseover', function() {
    var self = this;

    timer = setTimeout(function() {
      $(self).find('.action-viewer').show();
    }, 400);
  }).on('mouseleave', function() {
    clearTimeout(timer);
    $(this).find('.action-viewer').hide();
  });

});
body {
  padding: 15px
}
* {
  padding: 0;
  margin: 0;
}
.task-list {
  list-style: none
}
.task {
  padding: 10px;
  border-top: 1px solid #EFEFEF
}
.action-viewer {
  border-top: 1px solid #CCC
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="task-list">
  <li class="task">This is task number 01
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <li class="task">This is task number 02
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <li class="task">This is task number 03
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <li class="task">This is task number 04
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <li class="task">This is task number 05
    <div class="action-viewer"><a href="#">Action</a>
    </div>
  </li>
  <ul>

答案 1 :(得分:2)

使用hover()mouseenter/mouseleave的简写delay()方法:

jQuery(function ($) {
    var timer;

    $('.action-viewer').hide();

    $('.task').hover(function () {
        $(this).find('.action-viewer').delay(400).show(0);
    }, function () {
        $(this).find('.action-viewer').stop().hide();
    });

}); 

-jsFiddle-