Mar/083
Using MooTools to fade between two images
Fading between two images is actually a fairly simple task with MooTools. The first thing you need to do is setup up two DIV tags that are absolutely positioned over one another inside another DIV tag that uses default placement. Then you place your image tags inside each of the absolutely positioned DIV tags. Each of the DIV tags needs to have an id attribute so they can be referenced in your javascript code.
The HTML for the task will look like this:
1 2 3 4 5 6 | <pre> <div style="height:112px;" > <div id="div1" style="position:absolute; opacity: 0;"><img id="image1" src="image1.jpg" /></div> <div id="div2" style="position:absolute; opacity: 1;"><img id="image2" src="image2.jpg" /></div> </div> </pre> |
This gives you two DIV tags that will stack on top of one another but the DIV they are inside of will flow with the page. One of the DIV tags starts with an opacity of zero and the other with an opacity of one.
Next you will need a way to make one image disappear and the other appear. We do this by using the MooTools effects. MooTools allows you to animate the change of a style from one value to the next. The easiest way to do this is to use the effect member available on any element accessed with MooTools. To transition from one image to the other smoothly we animate the opacity of each DIV element in opposite directions one becoming fully opaque and the other becoming fully transparent. We do that with the following function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <pre> function show(whichOne){ if(whichOne == 1){ tDiv = "div1"; vDiv = "div2"; }else{ tDiv = "div2"; vDiv = "div1"; } if($(tDiv).fx){$(tDiv).fx.stop();} if($(vDiv).fx){$(vDiv).fx.stop();} $(tDiv).fx = $(tDiv).effect('opacity', {duration: 2000}).start(0); $(vDiv).fx = $(vDiv).effect('opacity', {duration: 2000}).start(1); } </pre> |
First you decide which DIV tag will become Transparent and which one will be visible. Next there are two lines of code to check for an active animation and stop any animations that are already running, before we start some new animations. The last two lines create an animation attached to each DIV, going from the current opacity to the specified Opacity, and sets the animation object to the DIV tags .fx member. See the full code below or check out the example.
Update: By popular demand I have created an new post that covers fading multiple images here: How to: Use MooTools to Fade Between Multiple Images
Mar/081
IE8 Arrogance
So I read this new IEBlog post (“Microsoft’s Interoperability Principles and IE8“) about how they have come to the decision that Internet Explorer 8 will render with IE8 standard mode by default. They say it like they are so smart for figuring that out, um… duh. Yes I would like to buy a new car, but could you please make it run like my old run down beater of a car, that would be great thanks. Then they say “…this choice creates a clear call to action to site developers to make sure their web content works well in IE”, WHAT!?!?!?!
HEY IE Team, how about this, you send out a clear call to action that site developers should develop to standards and your damn browser will render it properly.