We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

kif on 07/27/06


Tagged

event click mouse wheel


Versions (?)


Who likes this?

6 people have marked this snippet as a favorite

Roshambo
hides
sena
POPOEVER
shachi
hans


Mouse Event


Published in: JavaScript 


Support: Fx2, Opera9, IE6 Remodeled "Event.observe" of prototype.js... is necessary for this. This is indispensable for MouseWheel to support.

  1. /*-----------------------------------------------------------------------------
  2.  * Mouse Event
  3.  * Example Source
  4. Event.Mouse.right(window, function (e) {
  5. alert(Event.pointerX(e) + ', ' + Event.pointerY(e));
  6. });
  7. Event.Mouse.wheel(window, function (e, roll) {
  8. alert(roll);
  9. });
  10.  * Event Observer Sample
  11. Event.observe = function (element, name, observer, useCapture) {
  12. useCapture = useCapture || false;
  13. // Start Remodel
  14. name = (name == 'keypress' && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || window.attachEvent)) ? 'keydown' : (name == 'mousewheel' && !window.attachEvent) ? 'DOMMouseScroll' : name;
  15. // End Remodel
  16. if (element.addEventListener)
  17. element.addEventListener(name, observer, useCapture);
  18. else if (element.attachEvent)
  19. element.attachEvent('on' + name, observer);
  20. }
  21.  *-------------------------------------------------------------------------- */
  22.  
  23. var Mouse = {
  24. left: function (element, func) {
  25. Event.observe(element, 'click', func);
  26. },
  27. doubleleft: function (element, func) {
  28. Event.observe(element, 'dblclick', func);
  29. },
  30. right: function (element, func) {
  31. Event.observe(element, 'contextmenu', function (e) {
  32. func.apply(this, [e]);
  33. return false;
  34. })
  35. },
  36. wheel: function (element, func) {
  37. Event.observe(element, 'mousewheel', function (e) {
  38. func.apply(this, [e, (e.wheelDelta) ? e.wheelDelta / -120 : e.detail / 3]);
  39. Event.stop(e);
  40. });
  41. }
  42. }

Report this snippet 

You need to login to post a comment.