[thelist] Setting opacity with Javascript in IE7

ben morrison morrison.ben at gmail.com
Fri Jan 19 10:09:27 CST 2007


On 1/19/07, Mark Howells <webdev at mountain.ch> wrote:
> Can anyone suggest why this simple code doesn't work in IE7? (Yes,
> the element does exist.)
>
> document.getElementById("id1").filter="alpha(opacity=75)";
>
>   Setting the opacity sadly still has to be done with a filter and
> the equivalent CSS works fine.
>
> #id1 {filter:alpha(opacity=75);}

Try this:

setOpacity(document.getElementById("id1"), 75)

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
  obj.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="
+ opacity + ");";

  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;

  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}


ben
-- 
Ben Morrison



More information about the thelist mailing list