Placed in: Home arrow Programming arrow Webdesign arrow Theatrical movie trailer with jQuery
Theatrical movie trailer with jQuery

Don't you wonder sometimes, when looking at something, how you can create something like that yourself? I have to admit that I do. This is why I came up with the Skype for Mac and the iPhone examples in your webbrowser. Today, we're trying something new in this Proof of Concept category: creating a theatrical movie trailer with jQuery

Theatrical movie trailer with jQuery

This technique could be really well used when creating a slide-show that just needs some text. Using the CSS letter-spacing property, we can really make it look like the movies.

Demo Theatrical movie trailer   Download Theatrical movie trailer

I'll leave out the CSS in this tutorial, since not much really important stuff is going on there. This example works best in Firefox - Although it works in Safari and Opera too, the letter-spacing CSS property gets rounded up (or down) and you won't be able to see the full effect. Also, you'll need to imagine the sound effects yourself.

HTML

First, we'll need to give the page a backbone - the HTML. This is the structure that we're going to use in this tutorial.

 
<div id="theater">
      <div id="movieoverlay">
         <img src="images/play.png" alt="Play" class="playbutton" />
         <div id="overlaybg"></div>
      </div>
         
      <div class="preview prevtext">
         <p>In a world,<br />where fruits dominate the earth</p>
      </div>
      
      <div class="preview previmgwide">
         <img src="images/preview_01.jpg" alt="Preview 01" />
      </div>
 
      <div class="preview previmgheight">
         <img src="images/preview_02.jpg" alt="Preview 02" />
      </div>
</div>

Here's how it's going to work:

  • #movieoverlay - This is the starting screen - the overlay with the "play"-button.
  • .preview - Every preview element has this class applied. It's needed for CSS and jQuery to know that each of this divisions is a preview.
  • .prevtext - Another class that can be applied, is the prevtext class. We'll need this in jQuery to know that the division contains text that can be animated.
  • .previmgwide - A wide preview image contains this class. jQuery will animate the image from left to right.
  • .previmgheight - A high preview image contains this class. jQuery will animate the image from top to bottom.

The CSS file isn't that spectecular (just view the source yourself), so I suggest that we'll continue directly with the jQuery.

JavaScript

 
/* Time to preview a preview */
var PREVIEWTIME = 3000;
 
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function()
{
   // Set the opacity for the movie overlay (start screen)
   $("#overlaybg").fadeTo(0, 0.1);
   
   $("#movieoverlay").click(function(){
      $(this).fadeOut(function(){
         setTimeout("animatePreviews()", 500);   
      });   
   });
});
 
/* Variable to store which preview is currently viewed */
var currentPreview = -1;

This is what happens in the above piece of code:

  • PREVIEWTIME - A global variable that you can set to set the preview time on how long the animation of each preview should be.
  • $(this).fadeOut - Once the #movieoverlay has been clicked, it fades out. Using setTimeout, we can delay calling the animatePreviews function, that we'll create later.
  • currentPreview - A variable we need to remember which preview we are currently viewing. It's set to -1, since we'll increase it later. The eq-selector is 0-based, so when 1 is added to currentPreview, it'll give is 0; the first Preview element.

As you can see, we're calling a function called animatePreviews. Here's how this function looks like:

 
function animatePreviews() {
   currentPreview++;
   
   if( $(".preview:eq("+currentPreview+")").hasClass("prevtext") ) {
      animateTextPreview($(".preview:eq("+currentPreview+")"));
   } else if ( $(".preview:eq("+currentPreview+")").hasClass("previmgheight") ) {
      animateImagePreviewHeight($(".preview:eq("+currentPreview+")"));
   } else { 
      animateImagePreviewWide($(".preview:eq("+currentPreview+")"));
   }
}

  • currentPreview++ - Every time this function is called, 1 is added to the currentPreview variable. This way, we'll now which preview we're currently showing.
  • if (...) { } - The big if/elese statement simply checks which class the preview element has applied. If it's a text (prevtext), we'll call the function animateTextPreview and giving the element as an argument to the function.

One out of three functions can be called here: animateTextPreview, animateImagePreviewHeight and animateImagePreviewWide. This is how they look like:

 
function animateTextPreview(previewElement) {
   $(previewElement)
      .fadeIn("slow")
      .animate({
         letterSpacing : "3px"
      }, PREVIEWTIME)
      .fadeOut("slow", function(){
         animatePreviews();
      });
}
 
function animateImagePreviewWide(previewElement) {
   $(previewElement)
      .fadeIn("slow")
      .animate({ marginLeft : "-100px"} , PREVIEWTIME)
      .fadeOut("slow", function(){
         animatePreviews();
      });
}
 
function animateImagePreviewHeight(previewElement) {
   $(previewElement)
      .fadeIn("slow")
      .animate({ marginTop : "-100px"} , PREVIEWTIME)
      .fadeOut("slow", function(){
         animatePreviews();
      });
}

Explanation:

  • previewElement - Every function expects a previewElement that it can animate.
  • .fadeIn - First, the preview element is faded in.
  • .animate - Next, the preview element is animated. This is where the functions differ - The text gets another letter-spacing and the images are moving.
  • .fadeOut - Last, the preview element is faded out.
  • animatePreviews() - Once the element is faded out, it get's a callback to the animatePreview() function. This creates a loop to handle all the preview element.

And that's all we need to create a movie trailer using jQuery!

Conclusion and Download

Although this effect isn't that useful, I do think it's pretty nice to see to display text and images. Instead of calling three different functions to animate the element, one function would be enough too (with an if/else over there). But looking back, it's pretty cool to see!

Demo Theatrical movie trailer   Download Theatrical movie trailer

If you see any space to improve the code even more, be my guest! Do you think you could use this in one of your next projects?


Tags:  trailer jquery example proof of concept

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!Technorati!StumbleUpon!Newsvine!Furl!Ma.gnolia!
Comments
Add NewSearchRSS
Gaya   2009-09-21 17:58:19
Gravatar image Very cool!
We now need to kill those flash headers and implement this.
A bit choppy though (slower PC + Firefox).
Might be using something like this in the future. I see possibilities.
Yoosuf   2009-09-22 08:48:05
Gravatar image I am agree with you Gaya!, flash headers are really annoying!
Yoosuf - Amazing Work   2009-09-22 08:46:42
Gravatar image Seriously nice work, I am amazed and I am thinking of implementing for my next project. Keep it up such cool work!
Marco - Thanks a lot!   2009-09-22 14:11:36
Gravatar image Thanks a lot Yoosuf - glad you liked it. Hope to see this stuff more and more online - good luck!
walter - Great   2009-09-22 18:04:52
Gravatar image Very promising.

when watching the demo, the text animation seems a step-by-step animation... maybe it's firefox in my mac

good work!
Marco   2009-09-22 20:05:34
Gravatar image Thanks for the comment Walter! But strange that you say it looks like a "step-by-step" animation on Firefox. My FF on Mac (v3.5) is the only one that does display it non-'step-by-step'. Opera (9) and Safari (3.1) do display it "step-by-step".
Nick Lewis - Smoothing text   2009-09-22 18:28:18
Gravatar image It doesn't totally solve the issue, but if you lower the font size to something like 18px the growing animation is smoother. Though in all reality you would want that text to be a straight image if your going to grow it.
Marco - Thanks   2009-09-22 20:07:44
Gravatar image Thanks a lot for helping me (and others) improve the code Nick! There indeed is loads of space where this Proof of Concept could be improved, so be my guest. Just remember to "think outside the box" ;)
Josh L - Bravo!   2009-09-22 23:57:14
Gravatar image Best jQuery demo ever.
Featherstone   2009-09-25 18:26:01
Gravatar image :cheer: Right on. Your site is an inspiration to those of us on the brink of web programming greatness, and real asset and eye-opener to all. Thanks for this awesome post.
Anonymous - Histerical Great Thing   2009-10-01 02:56:49
Gravatar image Maannn !!! This is awesome... is anyone exited as me ? :lol:
Great... Thank you

Retweeted !
Frankie - movietrailer "player"   2009-10-15 12:26:08
Gravatar image Very nice !

Do you think this "player" could work with a converted version of mediabox advanced (http://iaian7.com/webcode/mediaboxAdvanced) ?

mediabox Advanced is using mootools 1.2.3. mediabox Advanced as jquery version with your "player" style would be nice.

Greetings,

Frank
Anirudh K. Mahant - WHERE CAN I SEE THE TEASER OF THIS MOVIE LOL   2010-01-23 12:45:56
Gravatar image WHERE CAN I SEE THE TEASER OF THIS MOVIE LOL!

Cool very very cool concept... Carrots Vs Fruits starring Keanu Reeves and Paris Hilton BIG LOL!
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio