[ Index ]

krapohl.info

title

Body

[close]

/pnincludes/ -> AnchorPosition.js (source)

   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. Instead,
  15  // please just point to my URL to ensure the most up-to-date versions
  16  // of the files. Thanks.
  17  // ===================================================================
  18  
  19  
  20  /* 
  21  AnchorPosition.js
  22  Author: Matt Kruse
  23  Last modified: 2/18/02
  24  
  25  DESCRIPTION: These functions find the position of an <A> tag in a document,
  26  so other elements can be positioned relative to it.
  27  
  28  COMPATABILITY: Works with Netscape 4.x, 6.x, IE 5.x on Windows. Some small
  29  positioning errors - usually with Window positioning - occur on the 
  30  Macintosh platform.
  31  
  32  FUNCTIONS:
  33  getAnchorPosition(anchorname)
  34    Returns an Object() having .x and .y properties of the pixel coordinates
  35    of the upper-left corner of the anchor. Position is relative to the PAGE.
  36  
  37  getAnchorWindowPosition(anchorname)
  38    Returns an Object() having .x and .y properties of the pixel coordinates
  39    of the upper-left corner of the anchor, relative to the WHOLE SCREEN.
  40  
  41  NOTES:
  42  
  43  1) For popping up separate browser windows, use getAnchorWindowPosition. 
  44     Otherwise, use getAnchorPosition
  45  
  46  2) Your anchor tag MUST contain both NAME and ID attributes which are the 
  47     same. For example:
  48     <A NAME="test" ID="test"> </A>
  49  
  50  3) There must be at least a space between <A> </A> for IE5.5 to see the 
  51     anchor tag correctly. Do not do <A></A> with no space.
  52  */ 
  53  
  54  // getAnchorPosition(anchorname)
  55  //   This function returns an object having .x and .y properties which are the coordinates
  56  //   of the named anchor, relative to the page.
  57  function getAnchorPosition(anchorname) {
  58      // This function will return an Object with x and y properties
  59      var useWindow=false;
  60      var coordinates=new Object();
  61      var x=0,y=0;
  62      // Browser capability sniffing
  63      var use_gebi=false, use_css=false, use_layers=false;
  64      if (document.getElementById) { use_gebi=true; }
  65      else if (document.all) { use_css=true; }
  66      else if (document.layers) { use_layers=true; }
  67      // Logic to find position
  68       if (use_gebi && document.all) {
  69          x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
  70          y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
  71          }
  72      else if (use_gebi) {
  73          var o=document.getElementById(anchorname);
  74          x=o.offsetLeft; y=o.offsetTop;
  75          }
  76       else if (use_css) {
  77          x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
  78          y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
  79          }
  80      else if (use_layers) {
  81          var found=0;
  82          for (var i=0; i<document.anchors.length; i++) {
  83              if (document.anchors[i].name==anchorname) { found=1; break; }
  84              }
  85          if (found==0) {
  86              coordinates.x=0; coordinates.y=0; return coordinates;
  87              }
  88          x=document.anchors[i].x;
  89          y=document.anchors[i].y;
  90          }
  91      else {
  92          coordinates.x=0; coordinates.y=0; return coordinates;
  93          }
  94      coordinates.x=x;
  95      coordinates.y=y;
  96      return coordinates;
  97      }
  98  
  99  // getAnchorWindowPosition(anchorname)
 100  //   This function returns an object having .x and .y properties which are the coordinates
 101  //   of the named anchor, relative to the window
 102  function getAnchorWindowPosition(anchorname) {
 103      var coordinates=getAnchorPosition(anchorname);
 104      var x=0;
 105      var y=0;
 106      if (document.getElementById) {
 107          if (isNaN(window.screenX)) {
 108              x=coordinates.x-document.body.scrollLeft+window.screenLeft;
 109              y=coordinates.y-document.body.scrollTop+window.screenTop;
 110              }
 111          else {
 112              x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
 113              y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
 114              }
 115          }
 116      else if (document.all) {
 117          x=coordinates.x-document.body.scrollLeft+window.screenLeft;
 118          y=coordinates.y-document.body.scrollTop+window.screenTop;
 119          }
 120      else if (document.layers) {
 121          x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
 122          y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
 123          }
 124      coordinates.x=x;
 125      coordinates.y=y;
 126      return coordinates;
 127      }
 128  
 129  // Functions for IE to get position of an object
 130  function AnchorPosition_getPageOffsetLeft (el) {
 131      var ol=el.offsetLeft;
 132      while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
 133      return ol;
 134      }
 135  function AnchorPosition_getWindowOffsetLeft (el) {
 136      return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
 137      }    
 138  function AnchorPosition_getPageOffsetTop (el) {
 139      var ot=el.offsetTop;
 140      while((el=el.offsetParent) != null) { ot += el.offsetTop; }
 141      return ot;
 142      }
 143  function AnchorPosition_getWindowOffsetTop (el) {
 144      return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
 145      }


Generated: Wed Feb 16 22:40:07 2005 Cross-referenced by PHPXref 0.6