我正在尝试动态添加一些DIVS(具有动态添加的功能)到CONTAINER. 当我点击其中的某个人时DIVs,MENU就是插入到这个DIV。mouseleave我需要在大约 5秒内从MENU这里移除。它有效,但混乱......所以我需要:DIVdelaysetInterval
当
mouseleave离开这个DIV然后我立即将鼠标放回这个DIV时,我需要停止delay或setInterval。我需要停止删除MENU.功能有问题
'handler'。因为当我添加一些时DIV,我CONTAINER第一次需要使用doubleclik, 来调用这个函数。但我需要使用简单的点击。
这是一个例子:http: //jsfiddle.net/ynternet/84nVQ/5/
HTML
<div id="add" style="background:yellow; width:100px;"> add new </div>
<div id="container"> </div>
<div id="menu" style="color:red;"><b> I'm here </b></div>
jQuery
function handler() {
$(this).on({
click: function(e) {
clearTimeout(time);
if ($(this).find("#menu").length) {
return;
}
$('#menu').prependTo($(this));
$("#menu").css({
position: "absolute",
left: "100px"
}).show();
}
});
$(this).on({
mouseleave: function(e) {
time = setTimeout(function() {
$('#menu').appendTo('body').clearTimeout(time);;
}, 5000);
}
});
$(this).on({
mouseover: function(e) {
clearTimeout(time);
}
});
}
$("#add").on({
click: function(e) {
var timestamp = Date.now();
var posx = Math.floor(Math.random()*400);
var posy = Math.floor(Math.random()*400);
$('#container').append(function() {
return $('<div class="add_to_this" id="' + timestamp + '" style="left:'+posx+'px; top:'+posy+'px; ">Click here</div>').click(handler);
});
}
});
CSS
#container {
width:500px;
height:500px;
background: palegoldenrod;
position: relative;
top:20px;
left: 100px;
padding: 0px;
}
.add_to_this {
background:yellowgreen;
position: absolute;
display:inline-block;
width:200px;
height:30px;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
user-select: none;
-o-user-select: none;
}