Previous Message
Next Message

PNG transparency alternative...?

Sent by brian ally on 18 December 2004 18:06


> The usual workarounds for PNG transparency in IE wouldn't quite work 
> here, as tiled backgrounds and the IE AlphaImageLoader hack don't work 
> together.

This hack does.

onload = function()
{
   if ( (/MSIE (5\.5)|[6789]/i.test(agent)) && (platform == 'win32') )
   {
     // call function here so that you may have other onload
     // procedures run for all browsers
     pngBGImage();
   }
}

/**
  * find elements which might need some help with
  * PNG transperency (IE only)
  *
  * @return void
  **/
function pngBGImage()
{
   var bgEl = document.getElementById('yourPNGBGElement');
   {
     setPNGBGImage(bgEl);

     // In soome cases, you may wish to set the elements visibility
     // to 'hidden' in an ie-only style sheet. Change that here.
     bgEl.style.visibility = 'visible';
   }

   // repeat calls to setPNGBGImage on any other elements...

}

/**
  * MSIE (still) cannot properly render PNGs with alpha transparency.
  * This function will set a filter to any element in need of such
  * first aid.
  *
  * @param element el
  * @return void
  **/
function setPNGBGImage(el)
{
   // NOTE: backslashes indicate text-wrapping

   // make this 1px transparent gif available somewhere
   var blankSrc = '/images/spacer.gif';

   // grab source for element bg
   var bgImageSrc = el.currentStyle.backgroundImage;

   // isolate the path (this could be more robust)
   bgImageSrc = bgImageSrc.substring( bgImageSrc.indexOf('url("') \
              + 5, bgImageSrc.indexOf('")') )

   // change bg to blank gif
   el.style.backgroundImage = 'url(' + blankSrc + ')';

   // set filter on blank gif. Notice that it is applying the
   // original bg image to the blank gif.
   el.runtimeStyle.filter = \
     "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" \
     + bgImageSrc + "',sizingMethod='scale')";
}

hth,

brian ally
______________________________________________________________________
css-discuss [EMAIL-REMOVED]]
http://www.css-discuss.org/mailman/listinfo/css-d
List wiki/FAQ -- http://css-discuss.incutio.com/
Supported by evolt.org -- http://www.evolt.org/help_support_evolt/
Previous Message
Next Message

Possibly related: