setTimeout-functionに少し問題があります。jquery timeout-functionがmouseenter mouseleaveで呼び出されない
$(this)は、特定のクラスを持つすべてのDOM要素です。
マウスがelememtに入ってから離しても問題ありません。しかし、マウスが要素を別の要素に直接渡すと(500msのタイムアウト時間内に)、最初の要素(その1つはマウスが左に残っています)がフェードアウトすることはありません。
したがって、新しい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"を使ってローカル変数をローカルとしてマークする必要があるのは私だけです。 –