Changing the opacity (transparency) of images using JavaScript

Submitted on: 1/3/2015 5:26:00 AM
By: John John (from psc cd)  
Level: Intermediate
User Rating: By 11 Users
Views: 1846
     See how CSS and JavaScript can be used to change the opacity of images.


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: //IE4 syntax //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:

function lightup(imageobject, opacity){
if (navigator.appName.indexOf("Netscape")!=-1&&parseInt(navigator.appVersion)>=5)
else if (navigator.appName.indexOf("Microsoft")!=-1&&parseInt(navigator.appVersion)>=4)
<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

 There are no comments on this submission.

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.