Using MooTools For Opacity

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

Although it's possible to achieve opacity using CSS, 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 to view an example. For those concerned with semantics, you can just as easily use the "title" attribute in the image.

References
0
Average: 2 (1 vote)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

dan tamas replied on Fri, 2008/03/28 - 7:03pm

The idea is nice, but is really usable only in case we use mootools for other stuff in that page. else, is just too much code to obtain this.

On the other side, 'semantic' guys will not be bothered by the fact that onmouseover 80, 60 etc appear as tooltip ?

By the way, the example uses alt :)

Maybe a selector like class="opacity_80" will be more suited in this case, and u keep the title and alt for its purpose...

Also an example using moo 1.11 would be good, as 1.2 is not out yet...

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.