home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / AnchorPosition.js < prev    next >
Encoding:
JavaScript  |  2004-07-12  |  5.3 KB  |  148 lines

  1. // ===================================================================
  2. // Author: Matt Kruse <matt@mattkruse.com>
  3. // WWW: http://www.mattkruse.com/
  4. //
  5. // NOTICE: You may use this code for any purpose, commercial or
  6. // private, without any further permission from the author. You may
  7. // remove this notice from your final code if you wish, however it is
  8. // appreciated by the author if at least my web site address is kept.
  9. //
  10. // You may *NOT* re-distribute this code in any way except through its
  11. // use. That means, you can include it in your product, or your web
  12. // site, or any other form where the code is actually being used. You
  13. // may not put the plain javascript up on your site for download or
  14. // include it in your javascript libraries for download. 
  15. // If you wish to share this code with others, please just point them
  16. // to the URL instead.
  17. // Please DO NOT link directly to my .js files from your site. Copy
  18. // the files to your server and use them there. Thank you.
  19. // ===================================================================
  20.  
  21. /* 
  22. AnchorPosition.js
  23. Author: Matt Kruse
  24. Last modified: 10/11/02
  25.  
  26. DESCRIPTION: These functions find the position of an <A> tag in a document,
  27. so other elements can be positioned relative to it.
  28.  
  29. COMPATIBILITY: Netscape 4.x,6.x,Mozilla, IE 5.x,6.x on Windows. Some small
  30. positioning errors - usually with Window positioning - occur on the 
  31. Macintosh platform.
  32.  
  33. FUNCTIONS:
  34. getAnchorPosition(anchorname)
  35.   Returns an Object() having .x and .y properties of the pixel coordinates
  36.   of the upper-left corner of the anchor. Position is relative to the PAGE.
  37.  
  38. getAnchorWindowPosition(anchorname)
  39.   Returns an Object() having .x and .y properties of the pixel coordinates
  40.   of the upper-left corner of the anchor, relative to the WHOLE SCREEN.
  41.  
  42. NOTES:
  43.  
  44. 1) For popping up separate browser windows, use getAnchorWindowPosition. 
  45.    Otherwise, use getAnchorPosition
  46.  
  47. 2) Your anchor tag MUST contain both NAME and ID attributes which are the 
  48.    same. For example:
  49.    <A NAME="test" ID="test"> </A>
  50.  
  51. 3) There must be at least a space between <A> </A> for IE5.5 to see the 
  52.    anchor tag correctly. Do not do <A></A> with no space.
  53. */ 
  54.  
  55. // getAnchorPosition(anchorname)
  56. //   This function returns an object having .x and .y properties which are the coordinates
  57. //   of the named anchor, relative to the page.
  58. function getAnchorPosition(anchorname) {
  59.     // This function will return an Object with x and y properties
  60.     var useWindow=false;
  61.     var coordinates=new Object();
  62.     var x=0,y=0;
  63.     // Browser capability sniffing
  64.     var use_gebi=false, use_css=false, use_layers=false;
  65.     if (document.getElementById) { use_gebi=true; }
  66.     else if (document.all) { use_css=true; }
  67.     else if (document.layers) { use_layers=true; }
  68.     // Logic to find position
  69.      if (use_gebi && document.all) {
  70.         x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
  71.         y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
  72.         }
  73.     else if (use_gebi) {
  74.         var o=document.getElementById(anchorname);
  75.         x=AnchorPosition_getPageOffsetLeft(o);
  76.         y=AnchorPosition_getPageOffsetTop(o);
  77.         }
  78.      else if (use_css) {
  79.         x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
  80.         y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
  81.         }
  82.     else if (use_layers) {
  83.         var found=0;
  84.         for (var i=0; i<document.anchors.length; i++) {
  85.             if (document.anchors[i].name==anchorname) { found=1; break; }
  86.             }
  87.         if (found==0) {
  88.             coordinates.x=0; coordinates.y=0; return coordinates;
  89.             }
  90.         x=document.anchors[i].x;
  91.         y=document.anchors[i].y;
  92.         }
  93.     else {
  94.         coordinates.x=0; coordinates.y=0; return coordinates;
  95.         }
  96.     coordinates.x=x;
  97.     coordinates.y=y;
  98.     return coordinates;
  99.     }
  100.  
  101. // getAnchorWindowPosition(anchorname)
  102. //   This function returns an object having .x and .y properties which are the coordinates
  103. //   of the named anchor, relative to the window
  104. function getAnchorWindowPosition(anchorname) {
  105.     var coordinates=getAnchorPosition(anchorname);
  106.     var x=0;
  107.     var y=0;
  108.     if (document.getElementById) {
  109.         if (isNaN(window.screenX)) {
  110.             x=coordinates.x-document.body.scrollLeft+window.screenLeft;
  111.             y=coordinates.y-document.body.scrollTop+window.screenTop;
  112.             }
  113.         else {
  114.             x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
  115.             y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
  116.             }
  117.         }
  118.     else if (document.all) {
  119.         x=coordinates.x-document.body.scrollLeft+window.screenLeft;
  120.         y=coordinates.y-document.body.scrollTop+window.screenTop;
  121.         }
  122.     else if (document.layers) {
  123.         x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
  124.         y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
  125.         }
  126.     coordinates.x=x;
  127.     coordinates.y=y;
  128.     return coordinates;
  129.     }
  130.  
  131. // Functions for IE to get position of an object
  132. function AnchorPosition_getPageOffsetLeft (el) {
  133.     var ol=el.offsetLeft;
  134.     while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
  135.     return ol;
  136.     }
  137. function AnchorPosition_getWindowOffsetLeft (el) {
  138.     return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
  139.     }    
  140. function AnchorPosition_getPageOffsetTop (el) {
  141.     var ot=el.offsetTop;
  142.     while((el=el.offsetParent) != null) { ot += el.offsetTop; }
  143.     return ot;
  144.     }
  145. function AnchorPosition_getWindowOffsetTop (el) {
  146.     return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
  147.     }
  148.