5,693,062 members and growing! (21,009 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Changing the opacity (transparency) of images using JavaScript

By John John

See how CSS and JavaScript can be used to change the opacity of images.
JavascriptWin2K, WinXP, Windows, Dev

Posted: 30 Sep 2002
Updated: 30 Sep 2002
Views: 91,465
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.10 Rating: 3.00 out of 5
1 vote, 25.0%
1
1 vote, 25.0%
2
0 votes, 0.0%
3
1 vote, 25.0%
4
1 vote, 25.0%
5

Introduction

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 semicolon:

<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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

John John



Location: United States United States

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralSome additionssussAnonymous3:23 29 Oct '04  
GeneralStandards compliant?editorPaul Watson22:31 30 Sep '02  
GeneralRe: Standards compliant?memberStephaneRodriguez23:01 30 Sep '02  
GeneralRe: Standards compliant?editorPaul Watson23:08 30 Sep '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Sep 2002
Editor: Smitha Vijayan
Copyright 2002 by John John
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project