1. /**
  2.  * Add a mouseenter event to an element.
  3.  * Event is fired once when cursor enter over the element or one of its children
  4.  *
  5.  * @param {element} el Element to attach event
  6.  * @param {Function} fn Function to execute when event is fired
  7.  * @param {object} obj Object passed as second argument to fn
  8.  * @param {object/boolean} overrideContext Scope of fn. If overrideContext == true, obj become the scope
  9.  *
  10.  */
  11. YAHOO.util.Event.onMouseEnter = function(el, fn, obj, overrideContext)
  12. {
  13. YAHOO.util.Event._mouseEnterLeave(el, fn, obj, overrideContext, 'mouseover');
  14. };
  15. /**
  16.  * Add a mouseleave event to an element.
  17.  * Event is fired once when cursor leave from the element or one of its children
  18.  *
  19.  * @param {element} el Element to attach event
  20.  * @param {Function} fn Function to execute when event is fired
  21.  * @param {object} obj Object passed as second argument to fn
  22.  * @param {object/boolean} overrideContext Scope of fn. If overrideContext == true, obj become the scope
  23.  *
  24.  */
  25. YAHOO.util.Event.onMouseLeave = function(el, fn, obj, overrideContext)
  26. {
  27. YAHOO.util.Event._mouseEnterLeave(el, fn, obj, overrideContext, 'mouseout');
  28. };
  29.  
  30. YAHOO.util.Event._mouseEnterLeave = function(el, fn, obj, overrideContext, eventtype)
  31. {
  32. YAHOO.util.Event.on(
  33. el,
  34. eventtype,
  35. function(e, obj)
  36. {
  37. if (e.relatedTarget != this &&
  38. !YAHOO.util.Dom.isAncestor(
  39. this,
  40. e.relatedTarget
  41. ))
  42. {
  43. if (!obj.overrideContext)
  44. {
  45. obj.overrideContext = this;
  46. }
  47. if (!obj.obj)
  48. {
  49. obj.obj = {};
  50. }
  51. if (obj.overrideContext === true)
  52. {
  53. obj.fn.apply(obj.obj);
  54. }
  55. else
  56. {
  57. obj.fn.apply(obj.overrideContext, obj.obj);
  58. }
  59. }
  60. },
  61. {
  62. fn: fn,
  63. obj: obj,
  64. overrideContext: overrideContext
  65. }
  66. );
  67. };
  68.  
  69. // On assigne un évenement de la sorte :
  70. YAHOO.util.Event.onMouseEnter(
  71. element,
  72. function(e, obj)
  73. {
  74. // Do something
  75. },
  76. params,
  77. overrideContext
  78. );
  79. YAHOO.util.Event.onMouseLeave(
  80. element,
  81. function(e, obj)
  82. {
  83. // Do something
  84. },
  85. params,
  86. overrideContext
  87. );

Bon, je ne suis pas vraiment satisfait de cette version mais ça semble fonctionner. Je suis à votre écoute pour toute modification. Proposez moi vos idées en commentaire ou par mail.

Edit : Voilà, je l'ai modifié. Elle est plus propre. Quoique pas assez testée. Vous pouvez voir un exemple ici. Je suis toujours ouvert à vos propositions.

Edit : Une 3° version mieux optimisée et qui marche pour de vrai :)

Edit : Une 4° pour que ça marche avec l'handicapé IE6 -_-,