[JavaScript] onMouseEnter and onMouseLeave Suppport for Old Browsers
onmouseenter and onmouseleave event are not supported in old browsers. If you need mouseenter or mouseleave event and want to support old browsers at the same time, this post shows how to do it.
Implementation of MouseEnter and MouseLeave
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | /** * Cross-browser addEventListener function. * * @param {DOM element} element The element to add event listener. * @param {string} evt The event to be listened. * @param {function} fn The callback function when event occurs. */ addEventListener_ = function(element, evt, fn) { if (window.addEventListener) { /* W3C compliant browser */ element.addEventListener(evt, fn, false); } else { /* IE */ element.attachEvent('on' + evt, fn); } }; /** * check if element is targer or the parent of target * @param {DOM Element} target * @param {DOM Element} element * @return {boolean} Return true if element is target or the parent of target * else return false. */ checkParent = function(target, element) { // Chrome and Firefox use parentNode, while Opera use offsetParent while(target.parentNode) { if( target == element ) return true; target = target.parentNode; } while(target.offsetParent) { if( target == element ) return true; target = target.offsetParent; } return false; }; /** * Cross-browser add mouse enter event listener. The mouse enter event only * fired if mouse enters the element, not fired if mouse enters child element(s) * of the registered element. * * @param {DOM element} element The element to add mouse enter event listener. * @param {function} fn The callback function when the mouse enter event occurs. */ addMouseEnterEventListener = function(element, fn) { var wrapper = function(e) { var evt = e || window.event; var targetElement = evt.target || evt.srcElement; // check if mouse moves inside the element, if yes, return. var relTarg = evt.relatedTarget || evt.fromElement; if (checkParent(relTarg, element)) return; setTimeout(fn, 0); }; addEventListener_(element, 'mouseover', wrapper); }; /** * Cross-browser add mouse leave event listener. The mouse leave event only * fired if mouse leaves the element, not fired if mouse leaves child element(s) * of the registered element. * * @param {DOM element} element The element to add mouse leave event listener. * @param {function} fn The callback function when the mouse leave event occurs. */ addMouseLeaveEventListener = function(element, fn) { var wrapper = function(e) { var evt = e || window.event; var targetElement = evt.target || evt.srcElement; // check if mouse moves inside the element, if yes, return. var relTarg = evt.relatedTarget || evt.toElement; if (checkParent(relTarg, element)) return; setTimeout(fn, 0); }; addEventListener_(element, 'mouseout', wrapper); }; |
Usage
Put the above code snippet in your JavaScript.
Register onmouseenter event handler sampleHandler to element sampleElement by:
addMouseEnterEventListener(sampleElement, sampleHandler);
Register onmouseleave event handler sampleHandler to element sampleElement by:
addMouseLeaveEventListener(sampleElement, sampleHandler);
Reference:
[1] | [JavaScript] Comparison of MouseEnter MouseLeave MouseOver MouseOut |