Important alert: (current site time 7/15/2013 11:52:42 AM EDT)
 

article

Changing the opacity (transparency) of images using JavaScript

Email
Submitted on: 10/1/2002 2:39:17 AM
By: John John  
Level: Intermediate
User Rating: By 11 Users
Compatibility: JavaScript
Views: 61999
 
     See how CSS and JavaScript can be used to change the opacity of images.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

Changing the opacity (transparency) of images using JavaScript

I think this technique is particularly useful and unique- using JavaScript to change the opacity of an image! The technique works in both IE4+ and NS6+, and can be used to create some interesting "fading" effects. Let's get started!

In IE4+, you can adjust the opacity of an image using the STYLE attribute:

<img src="ball.gif" style="filter:alpha(opacity=50)">

I've highlighted the main code. A value of 50 makes this image 50% oblique (or transparent). You can use a number between 0-100, where 0 would make the image disappear.

In NS6+, the code needed is a little bit different:

<img src="ball.gif" style="-moz-opacity:0.5">

Here the accepted range of values are 0 to 1, where 0 would make the image disappear.

You're probably now asking- how can I combine the two HTML above to make opacity specification work in both IE4+ and NS6+? Just define one STYLE attribute and put the two definitions inside it, separating the two with a semi colon:

<img src="ball.gif" style="filter:alpha(opacity=50); -moz-opacity:0.5">

Using JavaScript to alter opacity on the fly

This is where things get interesting and useful- using JavaScript to alter the value of the image's opacity! By doing so, you can make images fade in or out, for example.

The JavaScript syntax to change an image's opacity after it's been defined in the HTML is:

ImageObj.style.filters.alpha.opacity=90 //IE4 syntax
ImageObj.style.MozOpacity=0.9 //NS6 syntax

So for example, here's a simple script that adds a "lighting up" effect to your images as the mouse hovers over and out:

<script>
function lightup(imageobject, opacity){
if (navigator.appName.indexOf("Netscape")!=-1&&parseInt(navigator.appVersion)>=5)
imageobject.style.MozOpacity=opacity/100
else if (navigator.appName.indexOf("Microsoft")!=-1&&parseInt(navigator.appVersion)>=4)
imageobject.filters.alpha.opacity=opacity
}
</script>
<img src="test.gif" style="filter:alpha(opacity=50); -moz-opacity:0.5" 
onMouseover="lightup(this, 100)" onMouseout="lightup(this, 30)">

If you want to see a more complicated "lighting up" effect, check out Gradual highlight script by Dynamic Drive. It uses basically the same technique as I do, though the opacity is changed incrementally.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
10/2/2002 1:38:39 AMJosh Code

That's great to know. Its a really neat effect.
(If this comment was disrespectful, please report it.)

 
10/2/2002 4:41:44 AMAntonio Rodrigues

Short script that can be used for mouse-over effects with only one image!
Very cool, dude :)

Simpler than using behaviours from MS (more heavy than this one)...

(If this comment was disrespectful, please report it.)

 
10/2/2002 6:22:23 PM

mozilla also has a range from 0-100...
(If this comment was disrespectful, please report it.)

 
10/3/2002 12:37:46 AM

Really? Perhaps mozilla supports both integer and decimals (0.5) then.
(If this comment was disrespectful, please report it.)

 
10/5/2002 10:07:17 AM

exactly :)
mozilla simply rocks - as usual ;-)
(If this comment was disrespectful, please report it.)

 
10/7/2002 12:58:35 AMAkujin

if your interested, i have some very advanced usage of this on my prgressive website (which can be accessed through my profile). I studied all of this in depth using the microsoft API, and i've been using these techniques for a while now.
(If this comment was disrespectful, please report it.)

 
2/26/2003 6:58:57 PMPaul Cook

this is great !! good job !!
(If this comment was disrespectful, please report it.)

 
8/4/2004 3:26:31 AM

Wonderful script!
I would like to seek your approval to use your script for my company website.
(If this comment was disrespectful, please report it.)

 
8/4/2004 3:27:19 AM

Wonderful script! I would like to seek your approval to use your script for my company website.
(If this comment was disrespectful, please report it.)

 
2/9/2006 1:16:43 PMDaniel Morales (LeVampire)

This is Really Cool!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.