Crossfader v0.1 Documentation

Straight to the Demo »

I'm back at it. I figured I would post up how this thing works since I have had such a great response just from the demo. We first need to step back and really define what this thing is before we can start talking about what it can do and how I got the the demo.

Crossfader Defined

This script is designed to take a series of div containers all within a div and show their contents one by one. The first div is loaded when the page loads by which ever method you prefer, ruby, php, static html, it doesn't really matter. Each subsequent div is loaded after the page loads via ajax (This script uses the scriptaculous and prototype libraries). When we switch between one div and the next we perform a transition, not an effect. The transition in this case is a cross fade, hence the name. Now each div can contain anything, and I mean anything from a form to an image to a video if you want. I have also included a number of tools to allow you to build out player controls, add overlays into the transitions and also a navigational element (all shown in the demo). Whats even cooler about this thing is that css completely defines the look and feel of the crossfader.

Get the file (crossfader-0.1)

So lets dive right in!

The basics of this thing are pretty simple, first you need to include the prototype, scriptaculous, and crossfader files at the beginning of your file. Now that you have done that now lets see how we create the html for this bad boy.

HTML:

  1. <!-- start \\ foobar Crossfader -->
  2. <div id="foobar">         <!-- start \\ OPTIONAL Player Controls -->
  3. <div id="foobar-fade-box-controls" class="fade-box-controls">     <br />
  4. <a id="foobar-reverse" class="foobar-reverse" onclick="javascript: Crossfade.playerTools('foobar', 'reverse', null); Crossfade.togglePlayPause('foobar', 'pause');"><span>&laquo;</span></a>       <a id="foobar-play" class="foobar-play" onclick="javascript: Crossfade.playerTools('foobar', 'play', null); Crossfade.togglePlayPause('foobar', 'play');"><span>play</span></a>       <a id="foobar-pause" class="foobar-pause" onclick="javascript: Crossfade.playerTools('foobar', 'pause', null); Crossfade.togglePlayPause('foobar', 'pause');"><span>pause</span></a>     <a id="foobar-forward" class="foobar-forward" onclick="javascript: Crossfade.playerTools('foobar', 'forward', null); Crossfade.togglePlayPause('foobar', 'pause');"><span>&raquo;</span></a>  </div>
  5. <!-- end \\ OPTIONAL Player Controls -->                  <!-- start \\ Loading Animation -->   <img border="0" id="foobar-fade-box-loading" class="fade-box-loading" src="images/loading.gif" alt="" />  <!-- end \\ Loading Animation -->
  6. <div id="foobar-0" class="foobar-fade-box">            <!-- start \\ Contents -->         <!-- end \\ Contents -->                         </div>
  7. <!-- start \\ Additional Frames -->
  8. <div style="display: none;" id="foobar-1" class="foobar-fade-box">&nbsp;</div>
  9. <div style="display: none;" id="foobar-2" class="foobar-fade-box">&nbsp;</div>
  10. <div style="display: none;" id="foobar-3" class="foobar-fade-box">&nbsp;</div>
  11. <div style="display: none;" id="foobar-4" class="foobar-fade-box">&nbsp;</div>
  12. <!-- end \\ Additional Frames -->      <!-- start \\ OPTIONAL Tabs -->
  13. <div class="foobar-tabs">
  14.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 0); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-0-tab" class="foobar-tab-active"><span>Local</span></a></li>
  15.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 1); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-1-tab" class="foobar-tab"><span>Sports</span></a></li>
  16.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 2); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-2-tab" class="foobar-tab"><span>State</span></a></li>
  17.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 3); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-3-tab" class="foobar-tab"><span>Nation</span></a></li>
  18.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 4); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-4-tab" class="foobar-tab"><span>Special&nbsp;Somthin'</span></a></li>
  19. </ul>
  20. </div>
  21. <!-- end \\ OPTIONAL Tabs -->     </div>
  22. <!-- end \\ foobar Crossfader -->

So you will notice right off the bat there ae two optional blocks of code above for the Player Controls and Navigation Controls.   We will come back to these a little later.  

  1. Set up a container to hold the div frames as well as the optional Player Controls and Navigation Controls.   In the example above our container is

HTML:

  1. <div id="foobar">&nbsp;</div>

  • Next we need to set up a container for each of our frames(fade-box) starting with zero all placed in order in the container we created in step 1.   Say we would like 5 frames like in the demo...

    HTML:

    1. <div id="foobar-0" class="foobar-fade-box">&nbsp;</div>
    2. <div style="display: none;" id="foobar-1" class="foobar-fade-box">&nbsp;</div>
    3. <div style="display: none;" id="foobar-2" class="foobar-fade-box">&nbsp;</div>
    4. <div style="display: none;" id="foobar-3" class="foobar-fade-box">&nbsp;</div>
    5. <div style="display: none;" id="foobar-4" class="foobar-fade-box">&nbsp;</div>

    Notice how we set all of the frames(fade-box) to have a style of display: none except for the first one.   This has to be done as so that if javascript is enabled or not the first frame(fade-box) at least will show to the user.   We call this gracefully degrading a feature.

  • Below is the CSS Irequired, now the position is required to be set to absolute and you should set the height and width of the fade-box. Yes, this is all that you really need though if you want to make it pretty you should go the extra inches...

    CSS:

    1. /* Define Frame */ #foobar .fade-box{      /* Required */      position: absolute;      height: ###px;      width: ###px;      /* Required */ }

  • Now the fun part, we get to make it work!   Below is the chunk of code I used in the demo as i wanted the whole thing to start after the page loaded.

    JavaScript:

    1. function init(){          /*           container : string : id of container           url : string : url to pass the container # to in order to get the contents           count : integer : the number of containers 0-4=5           interval : the interval between transitions           totaloverlays : integer : the number of overlays           toggleoverlays : boolean : whether or not to toggle the overlays in and out of the transition           tabs : boolean : to use navigation or not        */      Crossfade.init('foobar', '/crossfader/jpg/', 5, 5000, 2, true, false)} window.onload = init;

  • Have a Beer or a glass of Wine...

    That's right, enjoy a tasty beverage and pat yourself on the back. What... it's not pretty enough? It works, isn't that enough? Ok ok...

    Overlays

    So overlays are pretty simple, they are pieces of text that you may want to overlay with a frame on transition or just place next to it or whatever (like the demo). The CSS is up to you but you get the idea from the demo, though there are two important things you have to do.

    1. Below is a sample fromt he demo of what I have for every frame, aside from the content changing.

      HTML:

      1. <img border="0" src="images/0.jpg" alt="" />
      2.     <div id="foobar-0-fade-box-overlay-0" class="fade-box-overlay-0">
      3.     <h1>Got Cow?</h1>
      4.     <h2>Have a cow! Farmers fawn over fair firsts</h2>
      5.     </div>
      6.     <div id="foobar-0-fade-box-overlay-1" class="fade-box-overlay-1">
      7.     <h1>Local</h1>
      8.     <ul>
      9.         <li><a href="#">Nearly $4 million in hops burn at storage facility</a></li>
      10.         <li><a href="#">14 Amish children die in schoolhouse slaying</a></li>
      11.         <li><a href="#">Cantwell, McGavick spar in heated debate</a></li>
      12.         <li><a href="#">NBA suspends Carmello, Robinson in latest fight</a></li>
      13.         <li><a href="#">Have a cow! Farmers fawn over fair firsts</a></li>
      14.     </ul>
      15.     </div>

      Notice how we set the class of the overlay div to fade-box-overlay-# where # is overlay set (in the example we have 2, 0 and 1) and the id is foobar-##-fade-box-overlay-# where ## is the frame number.

    2. Now just set the totaloverlays to something besides 0 and there you go. Now optionally you can set a true/false value to toggleoverlays to tell the crossfader to be included/excluded in the transition. Now I turn it on in my demo because one of the overlays is on a transparent layers and i cannot set a background color and in IE the anti-aliasing gets all mussed up otherwise (just a good example of when to turn it on).

    Player Controls

    Almost done, now we can talk about player controls. So there are two functions that do the meat of the work.

    JavaScript:

    1. /* This function actually runs the controls for the player, forward, reverse, or even goto a specific frame  container : string : id of container direction: string : 'forward' or 'reverse' or 'goto' frame : integer : the fade-box to 'goto' */ Crossfade.playerTools('foobar', 'reverse', null)/* This function actually toggles the play/pause button for a good interface so when someone presses play, it turns to pause or vice-versa  container : string : id of container button: string : 'play' or 'pause' */ Crossfade.togglePlayPause('foobar', 'pause');

    Below are the implementations in the demo that I used for the player controls and the tabs (yes the tabs are just player controls), it works well i think.

    HTML:

    1. <!-- start \\ Player Controls -->
    2. <div id="foobar-fade-box-controls" class="fade-box-controls">      <br />
    3. <a id="news-reverse" class="reverse" onclick="javascript: Crossfade.playerTools('foobar', 'reverse', null); Crossfade.togglePlayPause('foobar', 'pause');"><span>&laquo;</span></a>      <a id="news-play" class="play" onclick="javascript: Crossfade.playerTools('foobar', 'play', null); Crossfade.togglePlayPause('foobar', 'play');"><span>play</span></a>      <a id="news-pause" class="pause" onclick="javascript: Crossfade.playerTools('foobar', 'pause', null); Crossfade.togglePlayPause('foobar', 'pause');"><span>pause</span></a>      <a id="news-forward" class="forward" onclick="javascript: Crossfade.playerTools('foobar', 'forward', null); Crossfade.togglePlayPause('foobar', 'pause');"><span>&raquo;</span></a> </div>
    4. <!-- end\\ Player Controls -->    <!-- start \\ Tabs -->
    5. <div class="fade-box-tabs">
    6.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 0); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-0-tab" class="fade-box-tab-active"><span>Local</span></a></li>
    7.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 1); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-1-tab" class="fade-box-tab"><span>Sports</span></a></li>
    8.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 2); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-2-tab" class="fade-box-tab"><span>State</span></a></li>
    9.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 3); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-3-tab" class="fade-box-tab"><span>Nation</span></a></li>
    10.     <li><a onclick="javascript: Crossfade.playerTools('foobar', 'goto', 4); Crossfade.togglePlayPause('foobar', 'pause');" id="foobar-4-tab" class="fade-box-tab"><span>Special&nbsp;Somthin'</span></a></li>
    11. </ul>
    12. </div>
    13. <!-- end \\ Tabs -->

    The loading animation

    I threw this in behind the fade-box's for a nice effect, completely optional.

    Conclusion

    Now at this stage you are either supa happy or wondering why on earth you have to put html together and why its not generated. This is because this is a tool, not a pre-made piece of code. You really can do a lot with this depending on what you include and how you style it.

    Please offer up suggestions or improvements or ideas on some more demos I can throw together. Thanks guys, and check back soon!

    Colophon

    Kevin Miller I love Ruby on Rails, Prototype and Scriptaculous. Together we can do wonderful things, stick around and lets make some really cool stuff!

    Links


    Syndicate

    stickmanlabs feedburner

    Activity

    10 total comments, leave your comment or trackback.
    1. thanks
      very nice and sleek. good joobs.

      I would just apply some unobstructive events for the onclick..

    2. kevin.miller
      Jan 23rd 2007

      I had a couple in there but I didn’t see much use for them as far as the player goes and depending on what your building you may or may not want them. In the case of the demo ideally you would want the onclick event to take you to the story where as on a simple slide show you may just want a mouseover event to pause the show?

    3. Is there any way to determine how many recent comments it displays?

    4. kevin.miller
      Jan 29th 2007

      Judith-
      I am not sure what you are asking… comments it displays? Do you mean the overlay’s? Give me an example of what you are trying to do and I will see what I can do.

    5. Very nice,
      although it doesn’t work in IE6.
      It breaks down during the loading of the page.
      At some point you call Enumerable.each and it fails inside the try/catch.
      Very weird.
      I think it happens because either the dom or the images are not loaded yet.

      I noticed a similar error with the lightWindow when my browser doesn’t have a player for the media type and I get prompted for the download.

      here is the error.
      $(”lightWindow-photo-galleries”) has no properties
      http://localhost:3002/javascripts/lightWindow.js?1176997774
      Line 727
      if (this.galleryToggle) $(’lightWindow-photo-galleries’).style.display = ‘block’;

      the second statement should be in a try/catch.

    6. I would sure like to try your crossfader js, but I get an error page when I click on the download link:

      “Nothing to see here
      You seem to have found a mislinked file or page.”

      Hope you still have it available! It looks awesome .
      L.

    7. Hey,

      Great effects. Any way you can put the files back up? The link doesn’t seem to be working.

      Thanks.

    8. TommyK
      Dec 5th 2007

      I managed to find the link, since nothing has been done about it. It is here:
      http://stickmanlabs.com/crossfader/javascript/crossfader-0.1.js

    9. Russell
      Feb 25th 2008

      Can you please put the demo for this back up?

    1. January 22nd 2007

    Leave a Reply


    Search

    The archives run deep. Feel free to search older content using topic keywords.