home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 January / JAN2K_CHIP.ISO / dhtmllib.js < prev    next >
Text File  |  1999-12-10  |  10KB  |  445 lines

  1. /******************************************************************************
  2. * dhtmllib.js                                                                 *
  3. *                                                                             *
  4. * Copyright 1999 by Mike Hall.                                                *
  5. * Web address: http://www.brainjar.com                                        *
  6. * Last update: November 30, 1999.                                             *
  7. *                                                                             *
  8. * Provides basic functions for DHTML positioned elements which will work on   *
  9. * both Netscape Communicator and Internet Explorer browsers (version 4.0 and  *
  10. * up).                                                                        *
  11. ******************************************************************************/
  12.  
  13. // Determine browser.
  14.  
  15. var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 &&
  16.                 parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
  17. var isMinIE4 = (document.all) ? 1 : 0;
  18. var isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.")) >= 0 ? 1 : 0;
  19.  
  20. //-----------------------------------------------------------------------------
  21. // Layer visibility.
  22. //-----------------------------------------------------------------------------
  23.  
  24. function hideLayer(layer) {
  25.  
  26.   if (isMinNS4)
  27.     layer.visibility = "hide";
  28.   if (isMinIE4)
  29.     layer.style.visibility = "hidden";
  30. }
  31.  
  32. function showLayer(layer) {
  33.  
  34.   if (isMinNS4)
  35.     layer.visibility = "show";
  36.   if (isMinIE4)
  37.     layer.style.visibility = "visible";
  38. }
  39.  
  40. function isVisible(layer) {
  41.  
  42.   if (isMinNS4 && layer.visibility == "show")
  43.     return(true);
  44.   if (isMinIE4 && layer.style.visibility == "visible")
  45.     return(true);
  46.  
  47.   return(false);
  48. }
  49.  
  50. //-----------------------------------------------------------------------------
  51. // Layer positioning.
  52. //-----------------------------------------------------------------------------
  53.  
  54. function moveLayerTo(layer, x, y) {
  55.  
  56.   if (isMinNS4)
  57.     layer.moveTo(x, y);
  58.   if (isMinIE4) {
  59.     layer.style.left = x;
  60.     layer.style.top  = y;
  61.   }
  62. }
  63.  
  64. function moveLayerBy(layer, dx, dy) {
  65.  
  66.   if (isMinNS4)
  67.     layer.moveBy(dx, dy);
  68.   if (isMinIE4) {
  69.     layer.style.pixelLeft += dx;
  70.     layer.style.pixelTop  += dy;
  71.   }
  72. }
  73.  
  74. function getLeft(layer) {
  75.  
  76.   if (isMinNS4)
  77.     return(layer.left);
  78.   if (isMinIE4)
  79.     return(layer.style.pixelLeft);
  80.   return(-1);
  81. }
  82.  
  83. function getTop(layer) {
  84.  
  85.   if (isMinNS4)
  86.     return(layer.top);
  87.   if (isMinIE4)
  88.     return(layer.style.pixelTop);
  89.   return(-1);
  90. }
  91.  
  92. function getRight(layer) {
  93.  
  94.   if (isMinNS4)
  95.     return(layer.left + getWidth(layer));
  96.   if (isMinIE4)
  97.     return(layer.style.pixelLeft + getWidth(layer));
  98.   return(-1);
  99. }
  100.  
  101. function getBottom(layer) {
  102.  
  103.   if (isMinNS4)
  104.     return(layer.top + getHeight(layer));
  105.   else if (isMinIE4)
  106.     return(layer.style.pixelTop + getHeight(layer));
  107.   return(-1);
  108. }
  109.  
  110. function getPageLeft(layer) {
  111.  
  112.   if (isMinNS4)
  113.     return(layer.pageX);
  114.   if (isMinIE4)
  115.     return(layer.offsetLeft);
  116.   return(-1);
  117. }
  118.  
  119. function getPageTop(layer) {
  120.  
  121.   if (isMinNS4)
  122.     return(layer.pageY);
  123.   if (isMinIE4)
  124.     return(layer.offsetTop);
  125.   return(-1);
  126. }
  127.  
  128. function getWidth(layer) {
  129.  
  130.   if (isMinNS4) {
  131.     if (layer.document.width)
  132.       return(layer.document.width);
  133.     else
  134.       return(layer.clip.right - layer.clip.left);
  135.   }
  136.   if (isMinIE4) {
  137.     if (layer.style.pixelWidth)
  138.       return(layer.style.pixelWidth);
  139.     else
  140.       return(layer.clientWidth);
  141.   }
  142.   return(-1);
  143. }
  144.  
  145. function getHeight(layer) {
  146.  
  147.   if (isMinNS4) {
  148.     if (layer.document.height)
  149.       return(layer.document.height);
  150.     else
  151.       return(layer.clip.bottom - layer.clip.top);
  152.   }
  153.   if (isMinIE4) {
  154.     if (false && layer.style.pixelHeight)
  155.       return(layer.style.pixelHeight);
  156.     else
  157.       return(layer.clientHeight);
  158.   }
  159.   return(-1);
  160. }
  161.  
  162. function getzIndex(layer) {
  163.  
  164.   if (isMinNS4)
  165.     return(layer.zIndex);
  166.   if (isMinIE4)
  167.     return(layer.style.zIndex);
  168.  
  169.   return(-1);
  170. }
  171.  
  172. function setzIndex(layer, z) {
  173.  
  174.   if (isMinNS4)
  175.     layer.zIndex = z;
  176.   if (isMinIE4)
  177.     layer.style.zIndex = z;
  178. }
  179.  
  180. //-----------------------------------------------------------------------------
  181. // Layer clipping.
  182. //-----------------------------------------------------------------------------
  183.  
  184. function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) {
  185.  
  186.   if (isMinNS4) {
  187.     layer.clip.left   = clipleft;
  188.     layer.clip.top    = cliptop;
  189.     layer.clip.right  = clipright;
  190.     layer.clip.bottom = clipbottom;
  191.   }
  192.   if (isMinIE4)
  193.     layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
  194. }
  195.  
  196. function getClipLeft(layer) {
  197.  
  198.   if (isMinNS4)
  199.     return(layer.clip.left);
  200.   if (isMinIE4) {
  201.     var str =  layer.style.clip;
  202.     if (!str)
  203.       return(0);
  204.     var clip = getIEClipValues(layer.style.clip);
  205.     return(clip[3]);
  206.   }
  207.   return(-1);
  208. }
  209.  
  210. function getClipTop(layer) {
  211.  
  212.   if (isMinNS4)
  213.     return(layer.clip.top);
  214.   if (isMinIE4) {
  215.     var str =  layer.style.clip;
  216.     if (!str)
  217.       return(0);
  218.     var clip = getIEClipValues(layer.style.clip);
  219.     return(clip[0]);
  220.   }
  221.   return(-1);
  222. }
  223.  
  224. function getClipRight(layer) {
  225.  
  226.   if (isMinNS4)
  227.     return(layer.clip.right);
  228.   if (isMinIE4) {
  229.     var str =  layer.style.clip;
  230.     if (!str)
  231.       return(layer.style.pixelWidth);
  232.     var clip = getIEClipValues(layer.style.clip);
  233.     return(clip[1]);
  234.   }
  235.   return(-1);
  236. }
  237.  
  238. function getClipBottom(layer) {
  239.  
  240.   if (isMinNS4)
  241.     return(layer.clip.bottom);
  242.   if (isMinIE4) {
  243.     var str =  layer.style.clip;
  244.     if (!str)
  245.       return(layer.style.pixelHeight);
  246.     var clip = getIEClipValues(layer.style.clip);
  247.     return(clip[2]);
  248.   }
  249.   return(-1);
  250. }
  251.  
  252. function getClipWidth(layer) {
  253.  
  254.   if (isMinNS4)
  255.     return(layer.clip.width);
  256.   if (isMinIE4) {
  257.     var str = layer.style.clip;
  258.     if (!str)
  259.       return(layer.style.pixelWidth);
  260.     var clip = getIEClipValues(layer.style.clip);
  261.     return(clip[1] - clip[3]);
  262.   }
  263.   return(-1);
  264. }
  265.  
  266. function getClipHeight(layer) {
  267.  
  268.   if (isMinNS4)
  269.     return(layer.clip.height);
  270.   if (isMinIE4) {
  271.     var str =  layer.style.clip;
  272.     if (!str)
  273.       return(layer.style.pixelHeight);
  274.     var clip = getIEClipValues(layer.style.clip);
  275.     return(clip[2] - clip[0]);
  276.   }
  277.   return(-1);
  278. }
  279.  
  280. function getIEClipValues(str) {
  281.  
  282.   var clip = new Array();
  283.   var i;
  284.  
  285.   // Parse out the clipping values for IE layers.
  286.  
  287.   i = str.indexOf("(");
  288.   clip[0] = parseInt(str.substring(i + 1, str.length), 10);
  289.   i = str.indexOf(" ", i + 1);
  290.   clip[1] = parseInt(str.substring(i + 1, str.length), 10);
  291.   i = str.indexOf(" ", i + 1);
  292.   clip[2] = parseInt(str.substring(i + 1, str.length), 10);
  293.   i = str.indexOf(" ", i + 1);
  294.   clip[3] = parseInt(str.substring(i + 1, str.length), 10);
  295.   return(clip);
  296. }
  297.  
  298. //-----------------------------------------------------------------------------
  299. // Layer scrolling.
  300. //-----------------------------------------------------------------------------
  301.  
  302. function scrollLayerTo(layer, x, y, bound) {
  303.  
  304.   var dx = getClipLeft(layer) - x;
  305.   var dy = getClipTop(layer) - y;
  306.  
  307.   scrollLayerBy(layer, -dx, -dy, bound);
  308. }
  309.  
  310. function scrollLayerBy(layer, dx, dy, bound) {
  311.  
  312.   var cl = getClipLeft(layer);
  313.   var ct = getClipTop(layer);
  314.   var cr = getClipRight(layer);
  315.   var cb = getClipBottom(layer);
  316.  
  317.   if (bound) {
  318.     if (cl + dx < 0)
  319.  
  320.       dx = -cl;
  321.  
  322.     else if (cr + dx > getWidth(layer))
  323.       dx = getWidth(layer) - cr;
  324.     if (ct + dy < 0)
  325.  
  326.       dy = -ct;
  327.  
  328.     else if (cb + dy > getHeight(layer))
  329.       dy = getHeight(layer) - cb;
  330.   }
  331.  
  332.   clipLayer(layer, cl + dx, ct + dy, cr + dx, cb + dy);
  333.   moveLayerBy(layer, -dx, -dy);
  334. }
  335.  
  336. //-----------------------------------------------------------------------------
  337. // Layer background.
  338. //-----------------------------------------------------------------------------
  339.  
  340. function setBgColor(layer, color) {
  341.  
  342.   if (isMinNS4)
  343.     layer.bgColor = color;
  344.   if (isMinIE4)
  345.     layer.style.backgroundColor = color;
  346. }
  347.  
  348. function setBgImage(layer, src) {
  349.  
  350.   if (isMinNS4)
  351.     layer.background.src = src;
  352.   if (isMinIE4)
  353.     layer.style.backgroundImage = "url(" + src + ")";
  354. }
  355.  
  356. //-----------------------------------------------------------------------------
  357. // Layer utilities.
  358. //-----------------------------------------------------------------------------
  359.  
  360. function getLayer(name) {
  361.  
  362.   if (isMinNS4)
  363.     return findLayer(name, document);
  364.   if (isMinIE4)
  365.     return eval('document.all.' + name);
  366.  
  367.   return null;
  368. }
  369.  
  370. function findLayer(name, doc) {
  371.  
  372.   var i, layer;
  373.  
  374.   for (i = 0; i < doc.layers.length; i++) {
  375.     layer = doc.layers[i];
  376.     if (layer.name == name)
  377.       return layer;
  378.     if (layer.document.layers.length > 0) {
  379.       layer = findLayer(name, layer.document);
  380.       if (layer != null)
  381.         return layer;
  382.     }
  383.   }
  384.  
  385.   return null;
  386. }
  387.  
  388. //-----------------------------------------------------------------------------
  389. // Window and page properties.
  390. //-----------------------------------------------------------------------------
  391.  
  392. function getWindowWidth() {
  393.  
  394.   if (isMinNS4)
  395.     return(window.innerWidth);
  396.   if (isMinIE4)
  397.     return(document.body.clientWidth);
  398.   return(-1);
  399. }
  400.  
  401. function getWindowHeight() {
  402.  
  403.   if (isMinNS4)
  404.     return(window.innerHeight);
  405.   if (isMinIE4)
  406.     return(document.body.clientHeight);
  407.   return(-1);
  408. }
  409.  
  410. function getPageWidth() {
  411.  
  412.   if (isMinNS4)
  413.     return(document.width);
  414.   if (isMinIE4)
  415.     return(document.body.scrollWidth);
  416.   return(-1);
  417. }
  418.  
  419. function getPageHeight() {
  420.  
  421.   if (isMinNS4)
  422.     return(document.height);
  423.   if (isMinIE4)
  424.     return(document.body.scrollHeight);
  425.   return(-1);
  426. }
  427.  
  428. function getPageScrollX() {
  429.  
  430.   if (isMinNS4)
  431.     return(window.pageXOffset);
  432.   if (isMinIE4)
  433.     return(document.body.scrollLeft);
  434.   return(-1);
  435. }
  436.  
  437. function getPageScrollY() {
  438.  
  439.   if (isMinNS4)
  440.     return(window.pageYOffset);
  441.   if (isMinIE4)
  442.     return(document.body.scrollTop);
  443.   return(-1);
  444. }
  445.