Mouse.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function mouseCoords(ev){
  2. if(ev.pageX || ev.pageY){
  3. return {x:ev.pageX, y:ev.pageY};
  4. }
  5. if(ev.clientX || ev.clientY) {
  6. return {x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
  7. y:ev.clientY + document.body.scrollTop - document.body.clientTop
  8. };
  9. }
  10. return false;
  11. }
  12. function getMouseOffset(target, ev){
  13. ev = ev || window.event;
  14. var docPos = getPosition(target);
  15. var mousePos = mouseCoords(ev);
  16. //alert(mousePos.x+" "+mousePos.y);
  17. return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
  18. }
  19. function getPosition(e){
  20. var left = 0;
  21. var top = 0;
  22. while (e.offsetParent){
  23. //alert("x:"+e.offsetLeft+" y:"+e.offsetTop+" "+e.outerHTML);
  24. try{
  25. left += e.offsetLeft;
  26. top += e.offsetTop-e.scrollTop;
  27. }catch(ev){}
  28. e = e.offsetParent;
  29. }
  30. try{
  31. left += e.offsetLeft;
  32. top += e.offsetTop;
  33. }catch(ev){}
  34. return {x:left, y:top};
  35. }
  36. function Mouse() {
  37. this.checkRClick = function checkRClick (e) {
  38. if (!e)e = window.event;
  39. if ((e.button && e.button == 2) || (e.which && e.which == 3)) {
  40. return true;
  41. }else{
  42. return false;
  43. }
  44. }
  45. this.coords = function coords(ev){
  46. if(ev.pageX || ev.pageY){
  47. return {x:ev.pageX, y:ev.pageY};
  48. }
  49. if(ev.clientX || ev.clientY) {
  50. return {x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
  51. y:ev.clientY + document.body.scrollTop - document.body.clientTop
  52. };
  53. }
  54. return false;
  55. }
  56. }
  57. var Mouse = new Mouse();