Quick Search for:  in language:    
CSS,JavaScript,used,change,opacity,images
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Java/ Javascript Stats

 Code: 220,465. lines
 Jobs: 89. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Java/ Javascript.
Gobang
By Geniusbob Xu Qiang on 11/27


Click here to see a screenshot of this code!Salary
By Vikram Ivatury on 11/25

(Screen Shot)

Click here to see a screenshot of this code!A (part10) Powerful Swing Code to Maintain CD Database
By James Smith K on 11/25

(Screen Shot)

String Calculator
By MadokaCoder on 11/24


Chobi Dekha
By ShuvoRim on 11/23


Click here to see a screenshot of this code!A basic Client Server application II
By Ronald Holland on 11/23

(Screen Shot)

Bookmark image
By darren kurn on 11/22


myFT
By Owolabi Oyapero on 11/22


Click here to see a screenshot of this code!Simple Socket example
By Steven McElrea on 11/20

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Changing the opacity (transparency) of images using JavaScript

Print
Email
 

Submitted on: 10/1/2002 2:39:17 AM
By: John John  
Level: Intermediate
User Rating: By 9 Users
Compatibility:JavaScript

Users have accessed this article 7792 times.
 
 
     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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:Josh Code
That's great to know. Its a really neat effect.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/2/2002 4:41:44 AM:Antonio 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)...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/2/2002 6:22:23 PM:
mozilla also has a range from 0-100...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/3/2002 12:37:46 AM:
Really? Perhaps mozilla supports both integer and decimals (0.5) then.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/5/2002 10:07:17 AM:
exactly :) mozilla simply rocks - as usual ;-)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/7/2002 12:58:35 AM:Akujin
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/26/2003 6:58:57 PM:Paul Cook
this is great !! good job !!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Java/ Javascript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.