Showcase Slideshow With MooTools

One popular trend in web site home page layouts is a looping animation for featured stories. Sure, you could do it in Flash, but then you'd be stuck designing Casino web sites for the rest of you life. MooTools is powerful enough to make it look like Flash but retain the accessibility of (x)html.

Check out the demo or download the project files.

UPDATE: A new version of this script is now available.

Note: This demo is written using the MooTools 1.2 Beta Release.

The HTML

In my example I chose to display 4 total features but all you'd have to do to increase that is add another "showcase" div and its corresponding "showcase-nav" list item. Each big image div has a class called "bigimage" and an id with its number in the sequence. Note that numbers start at zero ("showcase-0-big", "showcase-1-big"...). The thumbnail navigation area doesn't require any numbering.

  1.  <div id="slideshow">
  2.    <div id="showcase">
  3.      <div id="showcase-loader">
  4.        <img src="images/ajax-loader-black.gif" width="32" height="32" alt="Loading" />
  5.      </div>
  6.      <div id="showcase-0-big" class="bigimage">
  7.        <div class="text">
  8.          <h2>Feature #1</h2>
  9.          <p>Some text... <a href="#">Some link...</a></p>
  10.        </div>
  11.        <img src="images/0.jpg" width="400" height="300" alt="" />
  12.      </div>
  13.      <div id="showcase-1-big" class="bigimage">
  14.        <div class="text">
  15.          <h2>Feature #2</h2>
  16.          <p>Some text... <a href="#">Some link...</a></p>
  17.        </div>
  18.        <img src="images/1.jpg" width="400" height="300" alt="" />
  19.      </div>
  20.      <div id="showcase-2-big" class="bigimage">
  21.        <div class="text">
  22.          <h2>Feature #3</h2>
  23.          <p>Some text... <a href="#">Some link...</a></p>
  24.        </div>
  25.        <img src="images/2.jpg" width="400" height="300" alt="" />
  26.      </div>
  27.      <div id="showcase-3-big" class="bigimage">
  28.        <div class="text">
  29.          <h2>Feature #4</h2>
  30.          <p>Some text... <a href="#">Some link...</a></p>
  31.        </div>
  32.        <img src="images/3.jpg" width="400" height="300" alt="" />
  33.      </div>
  34.    </div>
  35.    <div id="showcase-nav">
  36.      <ul>
  37.        <li><a href="#">
  38.          <span class="thumbnail"><img src="images/0-thumbnail.jpg" width="50" height="50" alt="" /></span>
  39.          <span class="text">Feature #1</span>
  40.        </a></li>
  41.        <li><a href="#">
  42.          <span class="thumbnail"><img src="images/1-thumbnail.jpg" width="50" height="50" alt="" /></span>
  43.          <span class="text">Feature #2</span>
  44.        </a></li>
  45.        <li><a href="#">
  46.          <span class="thumbnail"><img src="images/2-thumbnail.jpg" width="50" height="50" alt="" /></span>
  47.          <span class="text">Feature #3</span>
  48.        </a></li>
  49.        <li class="last"><a href="#">
  50.          <span class="thumbnail"><img src="images/3-thumbnail.jpg" width="50" height="50" alt="" /></span>
  51.          <span class="text">Feature #4</span>
  52.        </a></li>
  53.      </ul>
  54.    </div>
  55.  </div>

The Javascript

While the Mootools framework will handle the animation effects and browser compatibility issues, you'll need some custom javascript to get this showcase to work. This code (in showcase.js) includes starting events and 3 functions:

  • Starting Events
  • animation()
  • showshowcase()
  • activatethumbnail()

Starting Events

We need to do a couple things here: set the big images opacity to zero, setup the thumbnail navigation links, and start the animation. We're going to use a global variable called "number" to, you guessed it, keep track of which image we're on in the loop. Wondering what the difference between the "domready" and "load" events are? The "load" event fires only after all the images have finished loading - which is obviously very important for an image slideshow.

  1.  var number = 0;
  2.  window.addEvent('domready', function(){
  3.    $('showcase').getElements('.bigimage').each(function(i,x) {
  4.      i.setStyles({'opacity':'0'});
  5.    });
  6.    $('showcase-nav').getElements('li').each(function(i,x) {
  7.      i.addEvent('click', function(event){
  8.        event.stop();
  9.        showshowcase(x);
  10.        number = x;
  11.      });
  12.    });
  13.  });
  14.  window.addEvent('load', function(){
  15.    animation();
  16.  });

animation()

This function first checks to see if the number variable has reached the end of the loop. If so, it resets the number to zero. The next thing the function does is start a chain with three parts to the sequence: run the showcase function, iterate the number variable, and call itself recursively.

  1.  function animation() {
  2.    var nav = $('showcase-nav');
  3.    if (number > nav.getElements('li').length-1) {number = 0;}
  4.    var fx = nav.effects({duration: 1200, transition: Fx.Transitions.Quart.easeOut});
  5.    fx.start().chain(
  6.      function(){this.start(showshowcase(number));},
  7.      function(){this.start(number = number + 1);},
  8.      function(){this.start(animation());}
  9.    );
  10.  }

showshowcase()

The "showshowcase" function (no, I didn't stutter) is passed the number variable. The big image with the corresponding number is transitioned from zero opacity to fully opaque while the other big images are faded to transparent. This also calls the final function.

  1.  function showshowcase(number) {
  2.    activatethumbnail(number);
  3.    var big = 'showcase-' + number + '-big';
  4.    $('showcase-loader').setStyles({'display':'none'});
  5.    $('showcase').getElements('.bigimage').each(function(i) {
  6.      var fx = $(i).effects({duration: 1000, transition: Fx.Transitions.Quart.easeOut});
  7.      if (i.get('id') !== big) {
  8.        fx.start({'opacity':'0'});
  9.      }
  10.      else {
  11.        fx.start({'opacity':'1'});
  12.      }
  13.    });
  14.  }

activatethumbnail()

This sets an "active" class to the appropriate thumbnail nav item and calls it a day.

  1.  function activatethumbnail(number) {
  2.    $('showcase-nav').getElements('li').each(function(i,x) {
  3.      if (x == number) {
  4.        i.addClass('active');
  5.      }
  6.      else {
  7.        i.removeClass('active');
  8.      }
  9.    });
  10.  }

Final Thoughts

In future versions I may:

  • Add a scrolling thumbnail carousel - so it can contain more thumbnails in the same small space.
  • Add pause/resume functionality.

Incidentally, the transparent bar over the image? I was familiar with the CSS "opacity" property but that requires an additional (and non-standard) "alpha" property for IE. Also, it has a nasty habit of making every element inside it pseudo-transparent as well. After some investigation, it turns out the easiest solution is a transparent PNG background image. IE6 shows it as a solid color but it doesn't look half bad. A PNG transparency fix script could easily fix that if you were so inclined.

Comments

  • Felix 4 years ago

    Hi Jon, I would like to ask is it possible to edit it to make it work under Mootools 1.11?

    Thanks thanks.

  • Jon Plante 4 years ago

    I'm not familiar enough with 1.11 to answer that. Fundamentally, as long as you can set and transition opacity with 1.11, it's possible. The syntax may be slightly different though.

  • aldo 3 years ago

    hello, thank's a lot, i'm using it!

    I've troubles with mootools 1.2 final .. Have you tried it? I can't see photos and searching in the source code I found "visibility:hidden" :S

  • Jon Plante 3 years ago

    I have tried it with 1.2 final. But I've had some difficulty with the animation function. If I have time, I'll post some updated code.

  • aldo 3 years ago

    thank you so much Jon!

  • mandy 3 years ago

    Hi, thanks a lot for this plugin - I love it! I dont know much about javascript... but I would like to change the animation. I'd prefer to "stop" the automatic switch between images. I mean I want to show the visitor the first image, and then he can click through the others without having the animation switching around automatically. Is this possible? What do I need to change?

  • Jon Plante 3 years ago

    Mandy, all you would need to do to remove the animation is replace the "window.addEvent('load', function(){ animation();});" with "window.addEvent('load', function(){ showshowcase(0);
    });"

  • cssProdigy 3 years ago

    Exactly what i've been looking for, thanks.
    RELEASE a MooTools 1.2 Version soon!!

  • mandy 3 years ago

    Thank Jon, it works perfectly!!! :-)

  • sanjeev 3 years ago

    thanks.
    can any one tell me how to modify the code so that right hand side image and description of photos comes on left?

  • sanjeev 3 years ago

    can u also send me a extension so that there will be two arrows(one in top and the other in buttom) so that instead of 4 images one can scroll any no of images and display it.

  • Jon 3 years ago

    Great script!
    Is there a way to make the slideshow randomized rather than starting from the first slide each time the feature is loaded?

  • Jon Plante 3 years ago

    I would consider doing that server-side - put the images in an array and use the array_rand function.

  • Colin 3 years ago

    I love this unfortunately cant use it because it only works with the Beta version. It would be extremely beneficial to me to have this working with 1.2 Final. Is there a lot of work to get it working?

  • Jimmybee 2 years ago

    How does one change the vertical alignment of the "Featurex" text? I think I have tried every place I can see that would allow one to middle align the text.

    Thanks

  • chris 2 years ago

    Hello there jon, is it possible to have another selection when i click feature#1 then i will have another selection at the bottom of the picture?for example, i will click feature 1 then there will be a sub selection in it. is that possible?thank you.

  • Jon Plante 2 years ago

    You can put whatever you want (graphics, text, links) in the div with the class "text". If you're talking about additional thumbnail graphics appearing when you click feature#1 - which are then clickable to other large images in the slideshow - no, that is not possible without substantial modification of the script.

  • Rich 1 year ago

    This may be a stupid question, but if I wanted to put thumbnails on both sides, how would I go about doing that?

  • Jon Plante 1 year ago

    Rich, that could be done with some additional HTML markup and CSS. You'd first need to increase the width of the #slideshow div (to accommodate an extra column), then add the markup for the left column thumbnails, and finally, float the left column to the left with CSS. As long as the thumbnail options are named accordingly, the JavaScript wouldn't need to be modified at all.

  • Rich 1 year ago

    Thanks for your answer Jon. That is what I thought, and tried. I am sure I did something wrong, but when I increased the size of #slideshow and added the left column, the pictures quit loading and I was left with just the black ajax-loader pic. At least you verified I was on the right track, so I can keep troubleshooting from that point. Like I said, I am sure I missed something when I did it. Thanks again for an awesome gallery!

  • Rich 1 year ago

    Jon, I have been playing around with it, and I can get it to look right when make a duplicate #showcase-nav div (just called it #showcase-nav2) and float it left, but even though the images are named in the same way, it doesn't get put in the rotation. I guess that was somewhat expected. The thumbnails need to be inside the #showcase-nav div to work it seem, but I am not sure of how to tell some of them to float left, while keeping them inside that div.

  • Jon Plante 1 year ago

    That makes sense. You could try turning "showcase-nav" into a class (not an id), putting that on both the thumbnail areas, and then changing the $('showcase-nav') reference in the JavaScript to $$('.showcase-nav')

  • Rich 1 year ago

    Jon, I will try that. Let me see if I have it right, this is where my knowledge is fuzzy. Turn #showcase-nav into .showcase-nav, then I can specifiy both divs with that class. The reference in the javascript I assume is in the animation and/or thumbnail section and change it from $('showcase-nav') to $$('.showcase-nav').
    Correct?

  • Jon Plante 1 year ago

    Yes, just make sure every reference in the JavaScript gets changed to the new class selector, i.e. $$('.showcase-nav')

  • Rich 1 year ago

    I tried that, and now it just sits at the loading image animation. None of the big pictures show up and the rotation never starts.
    I changed from to

    I know I am close here, but I am obviously missing something simple.

  • Rich 1 year ago

    Ooops, forgot about no html. It was supposed to be that I changed div id="showcase-nav" to div class="showcase-nav"

  • Vishnukumar SR 1 year ago

    Here i had seen ur plugin script itz soo useful script for me.. i want to Implement this script in my website.. but urs in statically.i want this dynamically.

    plz can u help that how can i change it dynamically..

    Thanks jon
    by
    Vsihnu

  • Jon Plante 1 year ago

    Rich, it was a little more complicated than I thought. I was able to get it to work but HTML, CSS, and JavaScript needed to be modified. You view the modified slideshow here: http://jonplante.com/demo/showcase-3-col/

  • Rich 1 year ago

    Jon, you are a genius! Works like a charm! Thank you so very much!

  • Vishnukumar SR 1 year ago

    Hi Jon Plante

    I need this Script pulgin in dynamically.. can u plz help me Jon

  • Jon Plante 1 year ago

    What do you mean by "dynamically"? You want to generate the list from a server-side database? Or do you mean "randomly"?

  • Vishnukumar SR 1 year ago

    yes Jon i need to generate the list for a serverside database.. tell me how can we do this ? help me plz

  • Vishnukumar SR 1 year ago

    yes Jon i need to generate the list from a server-side database.. let me know how can we do this ? help me plz

  • Jon Plante 1 year ago

    Vishnukumar, unfortunately that's a complicated topic. Maybe an article like this could help: http://dev.mysql.com/tech-resources/articles/ddws/index.html

  • Vishnukumar SR 1 year ago

    Hi Jon

    This link is no use for me.. i need to generate ur suript in dynamically..
    Anyway thanks jon

  • Steve 1 year ago

    Hi Jon, thanks for a really useful slideshow tool.

    Is it possible to reduce the amount of time that loader gif shows prior to the slideshow beginning, independently of the duration of each slide? It seems that the two are linked and not independently adjustable, so that to allow time to read text in a slide you need to ensure the gif whirling away forever. Cheers.

  • Jon Plante 1 year ago

    The loader gif is only hidden when all the elements on your page are loaded (the mootools load event versus the domready event) - this is to ensure it doesn't start showing unloaded images. Depending on how many other elements are on your page, this could take some time.

    To remove this constrain, change

    window.addEvent('load', function(){
    animation();
    });

    to

    window.addEvent('domready', function(){
    animation();
    });

  • Sabin 1 year ago

    I've been looking for this same gallery for very long.

  • fourroses 11 months ago

    Hi Jon, when i click "really" fast on all the 4 buttons the images doesn't alpha that great, is there a way to prevent this?

    -> when animation is not yet done and u click on a other button.

    Beside that this slide is awesome.

    #edit, i see the other jQuery version doesn't have that problem :D

Comments are closed.

© 2012 Jon Plante. All Rights Reserved. Hosted by DreamHost. Send me a line, or check out my celebrity comics.