17
Mar/08
3

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>
&lt;div style="height:112px;" &gt;
    &lt;div id="div1" style="position:absolute; opacity: 0;"&gt;&lt;img id="image1" src="image1.jpg" /&gt;&lt;/div&gt;
    &lt;div id="div2" style="position:absolute; opacity: 1;"&gt;&lt;img id="image2" src="image2.jpg" /&gt;&lt;/div&gt;
&lt;/div&gt;
</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

[Post to Twitter]  [Post to Plurk]  [Post to Yahoo Buzz]  [Post to Delicious]  [Post to Digg]  [Post to Ping.fm]  [Post to Reddit]  [Post to StumbleUpon] 

Comments (2) Trackbacks (1)
  1. hgh56
    4:48 am on May 11th, 2008

    Many thanks for this script. I have a question how can we have multiple images in a same time. i tried diffrent methods like adding diffrent func and etc..

    none of them seems to work could you please advise me on this.

  2. Kelly
    12:18 pm on December 7th, 2008

Sorry, the comment form is closed at this time.

Tweet This Post links powered by Tweet This v1.3.9, a WordPress plugin for Twitter.