Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Using MooTools For Opacity

17 Responses »

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 rel tag. 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.get('rel'));
	});
});

The XHTML

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

The Result

Discussion

  1. March 27, 2008 @ 10:09 am

    Wait – mootools dropped the setOpacity(x); in 1.2? I’m disappointed now :(

  2. March 27, 2008 @ 10:17 am

    @keif: I’m pretty sure setOpacity works as well.

  3. March 27, 2008 @ 10:45 am

    I think you should be able to get any attribute you want (even a custom one, such as opacity) using el.getAttribute(“mycustomattribute”). I think that would work in this case and you wouldn’t have to worry about semantics. :)

  4. March 27, 2008 @ 11:55 am

    @Eric: That may be true, but I need to offend the semantics preachers, don’t I? :)

  5. jay
    March 27, 2008 @ 2:14 pm

    I’m kind of down on mootools, I have jumped ship for jquery. I just think it’s a more solid framework.

  6. jay
    March 27, 2008 @ 2:18 pm

    Ya you don’t want to use the alt tag, bc screen readers are going to read “80″ as the image name. That tool would be better off using some other property, maybe “opacity80″ or something. As devices/browsers/etc rely more on semantics to show html in various ways you really shouldn’t mis-appropriate attributes like that. For example if you have images turned off it would should “80″ –you wouldn’t do that in your browser but maybe that makes sense in a pda screen sometimes. I don’t know, I just think semantics should be followed.

  7. March 27, 2008 @ 2:32 pm

    I surrender!! The article now uses the title attribute.

  8. March 27, 2008 @ 2:47 pm

    I totally read this the wrong way. setOpacity can still be achieved, but the way you’re using it is to set different opacities on multiple elements.

    So, with that in mind, why not use the rel tag? :) Of course, you could also create a custom DTD so you’d validate. Or even incorporate the opacity into the class name, and use the same JS method to split the name and get your opacity level.

    My bad for being an idiot and not realizing the set_whatever has been replaced by a more simple set(_whatever_, ##)

  9. April 2, 2008 @ 10:02 pm

    I surrender even more. I’m now using the “rel” attribute.

  10. si
    July 1, 2008 @ 12:20 pm

    What is the correct way to implement an “onmouseover” opacity change to 100%?

  11. look trough mirror
    August 8, 2008 @ 12:28 pm

    Hi, I don´t know if this really work, can you explain me why are you using the attribute

    ‘ style=”opacity: 0.2;”
    style=”opacity: 0.4;”
    style=”opacity: 0.6;”

    in the image tag insted of really using the class and rel because I clean the code and find that this part is the only code that needs to set the opacity

  12. November 11, 2008 @ 8:32 pm

    ‘ style=”opacity: 0.2;” is not compatible to IE.
    for mootools 1.2 you should either do this:
    $(‘someID’).setStyle(‘opacity’, 0.2);
    or:
    $(‘someID’).set({opacity: 0.2});

  13. December 27, 2008 @ 2:19 am

    ‘ style=”opacity: 0.2;”
    style=”opacity: 0.4;”
    style=”opacity: 0.6;”

    this coad is not compatible for IE.This not only problem but also in drop down menu it works fine in
    div > ul >”li”

    but it does not work at
    div > “ul” background image in css.Have any solution?

  14. argama
    August 13, 2009 @ 11:57 am

    Is their a way to get the div set to opacity but not the content, only the background?

    background-color
    ?
    Thanks.

    (and thanks for this post)

  15. August 13, 2009 @ 12:01 pm

    Argama: RGBA is how that will be accomplished and will be in MooTools 2.

  16. argama
    August 14, 2009 @ 2:31 pm

    Ok, thanks for fast reply. :)

  17. mircea
    June 29, 2010 @ 4:29 pm

    @Jay: For me it`s the other way around :) . Jquery is just full of unnecessary code. At least let the developer do SOME things… so he can do it as it best fits his needs… if you want performance go with mootools

    A good framework gives you what you need to get the job done. It`s not supposed to do it for you (That`s just being lazy) because it cant possibly guess what you need, if it tries it ends up like this http://jqueryui.com/demos/datepicker/ . Just look at all those options you`d have to be crazy to actually use that, events spanned when and where you dont need them etc etc. (I`ve actually tried to use that once, when I was `younger` I ended up not being able to , as in I`ve modified it, because in all those options and events there isnt actually an htmlUpdated event. It`s just rubbish and way too bloated for my taste). BTW. the html is actually updated every time you select a date with that one… (mind you ,only in the `inline` version but stil .. is that crazy and unnecessary or what …)

    Just compare this two in ie6 (ie6 because it`s the best way to see the performance difference)
    http://www.snook.ca/technical/mootoolsdragdrop/
    http://jqueryui.com/demos/draggable/

    Just as a “shout” to jq, you`re giving starting developers more than enough rope to hang themselves with, so cut down on it. Everyone that uses jquery uses it because it`s simple not because it`s good. Anyway, I`ll stop my “love” for jq here

    P.S. there is an error in the mootools drag example that only shows in ie. It`s quite easy to fix you just have to pre-set `left` and `top`. If I`m not mistaken it has been fixed in the newer versions or it`s going to be fixed in the next one.

    P.P.S Take from this what you want. Everyone is free to choose whatever

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!