我对 setTimeout 函数有一点问题。
$(this)是具有特定类的每个 DOM 元素。
当鼠标进入一个元素,然后离开它,就没有问题了。但是当鼠标将一个元素直接离开另一个元素(在 500 毫秒超时内)时,第一个元素(即鼠标离开的那个元素)永远不会淡出。
因此,新的 mouseenter-Event 类型会阻止 timeOut 调用该函数。没有 setTimeout-wrapper 一切正常。
这是我的代码:
$(this).hover(methods['mouseenterManager'], methods['mouseleaveManager']);
/**
* manage mouseenter events
*/
mouseenterManager: function() {
clearTimeout(timer);
//create toolbar, if no toolbar is in dom
if ($(this).data("layouter").toolbar == undefined) {
//get bottom center of this element
pos_left = ($(this).width() / 2) + $(this).offset().left;
pos_top = $(this).height() + $(this).offset().top;
//create toolbar element
toolbar = $('<div style="display:none; left:' + parseInt(pos_left) + 'px; top:' + parseInt(pos_top) + 'px;" class="layouter_bar"><ul><li><a class="edit" href="javascript:;">Edit</a></li><li><a class="copy" href="javascript:;">Edit</a></li><li><a class="remove" href="javascript:;">Edit</a></li></ul></div>');
//bind this element to toolbar
toolbar.data("layouter", {
parent: $(this),
});
//bind toolbar to this element
data = $(this).data("layouter");
data.toolbar = toolbar;
$(this).data("layouter", data);
//bind this element to toolbar
data = toolbar.data("layouter");
data.parent = $(this);
toolbar.data("layouter", data);
element = $(this);
toolbar.mouseleave(function() {
toolbar = $(this);
timer = setTimeout(function() {
if (!toolbar.is(":hover") && !element.is(":hover")) {
toolbar.fadeOut("fast", function() {
$(this).remove();
});
data = element.data("layouter");
data.toolbar = undefined;
element.data("layouter", data);
}
}, 500);
});
//display the toolbar
$("body").append(toolbar);
toolbar.fadeIn("fast");
}
},
/**
* manage mouseleave events
*/
mouseleaveManager: function() {
toolbar = $(this).data("layouter").toolbar;
element = $(this);
if (toolbar != undefined) {
timer = setTimeout(function() {
if (!toolbar.is(":hover")) {
toolbar.fadeOut("fast", function() {
$(this).remove();
});
data = element.data("layouter");
data.toolbar = undefined;
element.data("layouter", data);
}
}, 500);
}
},
};
有任何想法吗?
谢谢你!