topical media & game development

talk show tell print

#javascript-code-10-fullHeight.js / js



  // Find the full, possible, height of an element (not the actual,
  // current, height)
  function fullHeight( elem ) {
      // If the element is being displayed, then offsetHeight
      // should do the trick, barring that, getHeight() will work
      if ( getStyle( elem, ‘display’ ) != ‘none’ )
          return elem.offsetHeight || getHeight( elem );
  
      // Otherwise, we have to deal with an element with a display
      // of none, so we need to reset its CSS properties to get a more
      // accurate reading
      var old = resetCSS( elem, {
          display: ‘’,
          visibility: ‘hidden’,
          position: ‘absolute’
      });
  
      // Figure out what the full height of the element is, using clientHeight
      // and if that doesn’t work, use getHeight
      var h = elem.clientHeight || getHeight( elem );
  
      // Finally, restore the CSS properties back to what they were
      restoreCSS( elem, old );
  
      // and return the full height of the element
      return h;
  }
  
  // Find the full, possible, width of an element (not the actual,
  // current, width)
  function fullWidth( elem ) {
      // If the element is being displayed, then offsetWidth
      // should do the trick, barring that, getWidth() will work
      if ( getStyle( elem, ‘display’ ) != ‘none’ )
          return elem.offsetWidth || getWidth( elem );
  
      // Otherwise, we have to deal with an element with a display
      // of none, so we need to reset its CSS properties to get a more
      // accurate reading
      var old = resetCSS( elem, {
          display: ‘’,
          visibility: ‘hidden’,
          position: ‘absolute’
      });
  
      // Figure out what the full width of the element is, using clientWidth
      // and if that doesn’t work, use getWidth
      var w = elem.clientWidth || getWidth( elem );
  
      // Finally, restore the CSS properties back to what they were
      restoreCSS( elem, old );
  
      // and return the full width of the element
      return w;
  }
  
  // A function used for setting a set of CSS properties, which
  // can then be restored back again later
  function resetCSS( elem, prop ) {
      var old = {};
  
      // Go through each of the properties
      for ( var i in prop ) {
          // Remember the old property value
          old[ i ] = elem.style[ i ];
  
          // And set the new value
          elem.style[ i ] = prop[i];
      }
  
      // Retun the set of changed values, to be used by restoreCSS
      return old;
  }
  
  // A function for restoring the side effects of the resetCSS function
  function restoreCSS( elem, prop ) {
      // Reset all the properties back to their original values
      for ( var i in prop )
          elem.style[ i ] = prop[ i ];
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.