MouseEnter/Leave Handlers


/ Published in: JavaScript
Save to your folder(s)

These functions allow you to easily set a handler for a virtual mouseleave/enter event.

This requires my [MouseBoundaryCrossing](http://snipplr.com/view/8206/crossbrowser-mouseenterleave-solution/) class.


Copy this code and paste it in your HTML
  1. /****************************************
  2. These functions allow you to easily set a handler for a virtual mouseleave/enter event, using my MouseBoundaryCrossing class.
  3. /****************************************/
  4.  
  5. //Note that a mouseout/over event is always fired before a mouseleave/enter event
  6. //Also note that mouseleave/enter events do not bubble; effectively, they don't bubble in this implementation either.
  7. //usage: elem.onmouseout = onMouseLeave(leaveHandler, outHandler);
  8. //usage: elem.onmouseover = onMouseEnter(enterHandler, overHandler);
  9. function onMouseLeave(handleLeave, handleOut)
  10. {
  11. if(!handleLeave) return handleOut;
  12. return function(evt)
  13. {
  14. evt = evt || window.event;
  15. if(handleOut) handleOut.call(this, evt);
  16. try{
  17. var mbc = new MouseBoundaryCrossing(evt, this);
  18. if(mbc.leftLandmark) handleLeave.call(this, evt);
  19. }catch(e){}
  20. }
  21. }
  22. function onMouseEnter(handleEnter, handleOver)
  23. {
  24. if(!handleEnter) return handleOver;
  25. return function(evt)
  26. {
  27. evt = evt || window.event;
  28. if(handleOver) handleOver.call(this, evt);
  29. try{
  30. var mbc = new MouseBoundaryCrossing(evt, this);
  31. if(mbc.enteredLandmark) handleEnter.call(this, evt);
  32. }catch(e){}
  33. }
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.