[英]Dropdown with jQuery mouseleave, mouseenter issue
My dropdown timeout not clear out when i hover the content area. 当我将鼠标悬停在内容区域时,下拉菜单超时不会清除。 I don't want to hide content area on hover.
我不想在悬停时隐藏内容区域。 Solve this i'm going really confuse this time.
解决这个问题,这次我真的很困惑。 HTML
的HTML
<button data-drp-hover="mynewid">HOVER</button>
<div class="drp-content" data-drp-content="mynewid">
<span>Item 1</span>
<span>Item 1</span>
<span>Item 1</span>
<span>Item 1</span>
<span>Item 1</span>
</div>
CSS 的CSS
[data-drp-content] {
display:none;
}
.drp-show[data-drp-content] {
display: block;
}
JS JS
$("[data-drp-hover]").mouseenter(function () {
var _drp_x = $(this);
var _drp_container = $(this).attr("data-drp-hover");
var _drp_content = $('[data-drp-content="' + _drp_container + '"]');
_drp_content.addClass("drp-show");
_drp_content.siblings().removeClass("drp-show");
$(document).click(function (event) {
_drp_content.removeClass("drp-show");
});
$(_drp_x, _drp_content).click(function (event) {
event.stopPropagation();
});
$(_drp_x, _drp_content).on("mouseleave", function () {
var timer = setTimeout(function () {
_drp_content.removeClass("drp-show");
}, 1000);
$(_drp_content).on("mouseenter", function () {
clearTimeout(timer);
});
});
});
Pen: https://codepen.io/anon/pen/GEvEZZ 笔: https : //codepen.io/anon/pen/GEvEZZ
Move the var timer to the top of your method. 将var计时器移到方法的顶部。
// Move the timer to here
var timer;
$("[data-drp-hover]").mouseenter(function () {
var _drp_x = $(this);
var _drp_container = $(this).attr("data-drp-hover");
var _drp_content = $('[data-drp-content="' + _drp_container + '"]');
_drp_content.addClass("drp-show");
_drp_content.siblings().removeClass("drp-show");
$(document).click(function (event) {
_drp_content.removeClass("drp-show");
});
$(_drp_x, _drp_content).click(function (event) {
event.stopPropagation();
});
$(_drp_x, _drp_content).on("mouseout", function () {
// Should not be var
timer = setTimeout(function () {
_drp_content.removeClass("drp-show");
}, 1000);
$(_drp_content).on("mouseenter", function () {
clearTimeout(timer);
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.