Mar
20
Posted on 2007-03-20
Filed Under (JavaScript) by Johan Känngård

Here’s a short snippet to set the opacity of an element:


function setOpacity(e,opacity){
  var o=e.style;
  o.opacity=(opacity/100); //Opera
  o.MozOpacity=(opacity/100); //Mozilla+Firefox
  o.KhtmlOpacity=(opacity/100); //Konqueror
  o.filter="alpha(opacity="+opacity+")"; //IE
}

Use it like this:

var e=document.getElementById('myElement');
setOpacity(e, 50);//Sets the opacity to 50%

[tags]JavaScript[/tags]

    Read More   

Comments

Patrick Kwinten on 21 March, 2007 at 15:10:34 #

I would add a little timer to the opacity effect:

/* Effects: Fade effect for images */

var baseopacity=50

function slowhigh(which2){
imgobj=which2
browserdetect=which2.filters? “ie” : typeof which2.style.MozOpacity==”string”? “mozilla” : “”
instantset(baseopacity)
highlighting=setInterval(”gradualfade(imgobj)”,50)
}

function slowlow(which2){
cleartimer()
instantset(baseopacity)
}

function instantset(degree){
if (browserdetect==”mozilla”)
imgobj.style.MozOpacity=degree/100
else if (browserdetect==”ie”)
imgobj.filters.alpha.opacity=degree
}

function cleartimer(){
if (window.highlighting) clearInterval(highlighting)
}

function gradualfade(cur2){
if (browserdetect==”mozilla” && cur2.style.MozOpacity


Johan Känngård on 21 March, 2007 at 15:41:38 #

That’s cool. You could also use Script.aculo.us’ Effect.Opacity


Post a Comment
Name:
Email:
Website:
Comments: