DZone: Your Personal Tech Universe Web Builder Zone
Published on Web Builder Zone (http://css.dzone.com)
Using MooTools For Opacity
By davidwalsh
Created 2008/03/27 - 9:01am

Update: The semantic-lovers have won!  I'm now using the title attribute.

Although it's possible to achieve opacity using CSS [1], the hacks involved aren't pretty. If you're using the MooTools javascript library, opacity is as easy as using an element's "set" method. The following MooTools snippet takes every image with the "opacity" class and sets the element's opacity based upon the number in the image's "alt" tag (correct, semantics have been thrown out the window). MooTools 1.2 is required.

The MooTools Javascript

/* on dom ready ... */
window.addEvent('domready', function() {
/* for each image that requires opacity */
$$('.opacity').each(function(el) {
/* set the opacity based on the alt value */
el.set('opacity','.' + el.getProperty('title'));
});
});

The XHTML

<img src="rod.jpg" title="80" class="opacity" />
<img src="rod.jpg" title="60" class="opacity" />
<img src="rod.jpg" title="40" class="opacity" />
<img src="rod.jpg" title="20" class="opacity" /><br />

The Result

Click here [2] to view an example. For those concerned with semantics, you can just as easily use the "title" attribute in the image.

Reference: 
Using MooTools For Opacity [3]

Source URL: http://css.dzone.com/news/using-mootools-opacity

Links:
[1] http://davidwalsh.name/cross-browser-compatible-css-opacity
[2] http://davidwalsh.name/dw-content/moo-opacity.php
[3] http://davidwalsh.name/mootools-opacity