Geometry.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Geometry.js: portable functions for querying window and document geometry
  3. *
  4. * This module defines functions for querying window and document geometry.
  5. *
  6. * getWindowX/Y(): return the position of the window on the screen
  7. * getViewportWidth/Height(): return the size of the browser viewport area
  8. * getDocumentWidth/Height(): return the size of the document.
  9. * getHorizontalScroll(): return the position of the horizontal scrollbar
  10. * getVerticalScroll(): return the position of the vertical scrollbar
  11. *
  12. * Note that there is no portable way to query the overall size of the
  13. * browser window, so there are no getWindowWidth/Height() functions.
  14. *
  15. * IMPORTANT: This module must be included in the <body> of a document
  16. * instead of the <head> of the document.
  17. */
  18. var Geometry = {};
  19. if (window.screenLeft) { // IE and others
  20. Geometry.getWindowX = function() { return window.screenLeft; };
  21. Geometry.getWindowY = function() { return window.screenTop; };
  22. }
  23. else if (window.screenX) { // Firefox and others
  24. Geometry.getWindowX = function() { return window.screenX; };
  25. Geometry.getWindowY = function() { return window.screenY; };
  26. }
  27. if (window.innerWidth) { // All browsers but IE
  28. Geometry.getViewportWidth = function() { return window.innerWidth; };
  29. Geometry.getViewportHeight = function() { return window.innerHeight; };
  30. Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
  31. Geometry.getVerticalScroll = function() { return window.pageYOffset; };
  32. }
  33. else if (document.documentElement && document.documentElement.clientWidth) {
  34. // These functions are for IE6 when there is a DOCTYPE
  35. Geometry.getViewportWidth =
  36. function() { return document.documentElement.clientWidth; };
  37. Geometry.getViewportHeight =
  38. function() { return document.documentElement.clientHeight; };
  39. Geometry.getHorizontalScroll =
  40. function() { return document.documentElement.scrollLeft; };
  41. Geometry.getVerticalScroll =
  42. function() { return document.documentElement.scrollTop; };
  43. }
  44. else {//remove if condition by Ye
  45. // These are for IE4, IE5, and IE6 without a DOCTYPE
  46. try{
  47. Geometry.getViewportWidth =
  48. function() { return document.body.clientWidth; };
  49. Geometry.getViewportHeight =
  50. function() { return document.body.clientHeight; };
  51. Geometry.getHorizontalScroll =
  52. function() { return document.body.scrollLeft; };
  53. Geometry.getVerticalScroll =
  54. function() { return document.body.scrollTop; };
  55. }catch(e){
  56. }
  57. }