我有一个小setTimeout函数的问题。jquery超时功能从来没有呼吁mouseenter mouseleave
$(this)是每个具有特定类的DOM元素。
当鼠标进入elememt,然后离开它时,没有问题。但是当鼠标直接将一个元素留给另一个元素(在500ms超时时间内)时,第一个元素(即那个,鼠标离开的元素)永远不会淡出。
因此,新的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);
}
},
};
任何想法?
谢谢!
谢谢!这是一个简单的全球/本地问题!我需要通过使用“var”将局部变量标记为本地变量。 – 2012-04-18 12:30:38