html
<div id="div1" style="background: aqua;width: 50px;height: 50px;">
</div>
<div id="div2" style="background: navy;width: 100px;height: 100px;"">
</div>
js
$("#div1").mouseenter(function(e){
$("#div2").slideDown(500);
$("#div1").mouseleave(function(e){
var array = new Array();
array.push($("#div1")[0]);
array.push($("#div2")[0]);
var flag = checkIn(e,array)
if (!flag) {
$("#div2").slideUp(500);
}
});
$("#div2").mouseenter(function(e){
$("#div2").mouseleave(function(e){
var array = new Array();
array.push($("#div1")[0]);
array.push($("#div2")[0]);
var flag = checkIn(e,array)
if (!flag) {
$("#div2").slideUp(500);
}
});
});
});
function checkIn(e,objs){
try {
console.log(objs.length);
} catch (e) {
alert(1);
}
var x = e.clientX;
var y = e.clientY;
for (i = 0; i < objs.length; i++) {
var obj = objs[i];
if (x > obj.offsetLeft && x < (obj.offsetLeft + obj.clientWidth) && y > obj.offsetTop && y < (obj.offsetTop + obj.clientHeight)) {
return true;
} else {
}
}
return false;
}
初始化隐藏效果我没写,你自己加上就好