Skip to content

Instantly share code, notes, and snippets.

@liunian
Last active April 26, 2024 03:30
Show Gist options
  • Save liunian/7524406 to your computer and use it in GitHub Desktop.
Save liunian/7524406 to your computer and use it in GitHub Desktop.
mouseenter / mouseleave
function bindMouseLeave(obj, fn) {
if (obj.attachEvent) {
obj.attachEvent('onmouseleave', function() {
fn.call(obj, window.event);
});
} else {
obj.addEventListener('mouseout', function(e) {
var rt = e.relatedTarget;
if (rt !== obj && !isContain(obj, rt)) {
fn.call(obj, e);
}
},
false);
}
}
function bindMouseEnter(obj, fn) {
if (obj.attachEvent) {
obj.attachEvent('onmouseenter', function() {
fn.call(obj, window.event);
});
} else {
obj.addEventListener('mouseover', function(e) {
var rt = e.relatedTarget;
if (rt !== obj && !isContain(obj, rt)) {
fn.call(obj, e);
}
},
false);
}
}
function isContain(a, b) {
try {
return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
}catch (e) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment