Jump to content

Javascript Opacity

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Howdy_McGee

Howdy_McGee

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
I'm trying to do something as simple as making a text or a colored box opaque on click using javascript. Some thing simple to see how exactly it works to do so. I tried doing it using a loop but had no success.

I was wondering if you guys could show me an example and maybe help me understand how it works exactly?

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Most functions you see will edit the elements CSS property. I posted a link to w3's school, this shows the CSS and JS examples of how to edit a property's opacity.

CSS Image Opacity / Transparency

#3
Howdy_McGee

Howdy_McGee

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
Oh yeah I've tried that and putting it into a loop. What i'm trying to do it have it "Fade" with opacity. Like the opacity slowly disappears. I'm just not sure how to code that.

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Works fine for me.
<!DOCTYPE HTML>
<html>
    <head>
        <title>Fading JS</title>
    </head>
    <body>
        <div id="fadeMe">
            I'm MELTING!!!
        </div>
        <script type="text/javascript">
            function fadeID(id, opacity) {
                document.getElementById(id).style.opacity=opacity/10;
                document.getElementById(id).filters.alpha.opacity=opacity*10;    
            }
            var a = 10;
            for(var i = 0;i<10;i++) {
                setTimeout("fadeID('fadeMe', "+i+");", 500*a--); // Fade out
                // setTimeout("fadeID('fadeMe', "+i+");", 500*i); // Fade In
            }
        </script>
    </body>
</html>
JS Libraries are the norm for websites. You may check out jQuery
.fadeIn() – jQuery API
.fadeOut() – jQuery API

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
There seems to be a nice writeup here:
Javascript Fade Effects :: I Like Spam

The key codes being
 var element = document.getElementById('somediv');
function setOpacity(level) {
  element.style.opacity = level;
  element.style.MozOpacity = level;
  element.style.KhtmlOpacity = level;
  element.style.filter = "alpha(opacity=" + (level * 100) + ");";
}
And
[FONT=Courier New]var duration = 1000;  /* 1000 millisecond fade = 1 sec */
var steps = 20;       /* number of opacity intervals   */[/FONT][FONT=Courier New]
var delay = 5000;     /* 5 sec delay before fading out */

function fadeIn(){
  for (i = 0; i <= 1; i += (1 / steps)) {
    setTimeout("setOpacity(" + i + ")", i * duration);
  }
  setTimeout("fadeOut()", delay);
}

function fadeOut() {
  for (i = 0; i <= 1; i += (1 / steps)) {
    setTimeout("setOpacity(" + (1 - i) + ")", i * duration);
  }
  setTimeout("fadeIn()", duration);
}[/FONT]
Edit: beaten to it!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users