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!StumbleUpon!
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!
peoples pharmacy info - peoples pharmacy info   2011-06-14 06:10:52
Gravatar image You guys are really wonderful who search and bring such a wonderful info, I am glad to see this time also a useful stuff that had inspired me. Thanks a lot do keep giving us genuine stuff
great anti smoking pills - great anti smoking pills   2011-06-14 06:12:07
Gravatar image I enjoy reading the post and have become a great fan of yours. Keep up with the good job and please provide us with great blogs. I really appreciate the research you people take for the posts.
best pharmacy tips - best pharmacy tips   2011-06-14 06:12:41
Gravatar image It was perfect collection of such useful information. This was a helpful post foe me. Thanks for sharing such nice information. Thanks a lot!
better pharmacy spot - better pharmacy spot   2011-06-14 06:13:46
Gravatar image I was a great experience to read your post. I found your site from Google and thank a lot for this nice and wonderful information. The information posted was useful and interesting.
cheap discount pharmacy spot - cheap discount pharmacy spot   2011-06-14 06:14:19
Gravatar image Thanks a lot for such a wonderful post, the stuff posted were really interesting and useful. The quality of the content was good and clear. Thanks for the post
cheap anti smoking pills - cheap anti smoking pills   2011-06-14 06:14:57
Gravatar image I appreciate the ideas posted on your site; they were very informative and innovative. It was worth visiting your site. Thanks a lot for such valuable information.
best pharmacy diary - best pharmacy diary   2011-06-14 06:15:26
Gravatar image It is amazing to such useful information at one place. I was looking for the same information from a long time, at last I found it. Thanks for such innovative and amazing information.
online diabetes pills club - online diabetes pills club   2011-06-14 06:15:57
Gravatar image I was very pleased to visit your site; I was definitely a wonderful site. The post was worth reading. I enjoyed each bit of your post. Thanks for such excellent post.
overweight pill - overweight pill   2011-06-14 06:17:43
Gravatar image I am extremely happy to write a comment on this blog. The information provided on the site was very informative and useful. Thanks a lot for such needy and innovative ideas and concepts.
best deals pharmacy online - best deals pharmacy online   2011-06-14 06:18:15
Gravatar image I really want to thank yahoo search engine who have sent me to this wonderful site. The articles posted on this site are great and amazing. I am glad that I have found this amazing site.
online hair loss pacts - online hair loss pacts   2011-06-14 06:18:48
Gravatar image I found this site extremely useful and informative. It is full of all useful stuff and I am glad that I have come across this site. I have made by job much easier, thanks for sharing with us.
diabetes pills reviews - diabetes pills reviews   2011-06-14 06:19:23
Gravatar image Wonderful post. I like the way it is written and presented. I have book marked the site for my future reference.
legal online pharmacy store - legal online pharmacy store   2011-06-14 06:19:59
Gravatar image Nice blog and the article are simply superb. The article posted was very informative and useful. You people are doing a great job. Keep going.
liquid diet side effects repor - liquid diet side effects report   2011-06-14 06:20:29
Gravatar image I had logged onto this site very recently and found it to be very useful and informative. Each and every concept was well presented. Thanks a lot for the information.
Rescue Leokeng - Fantastics   2011-09-16 13:18:03
Gravatar image Please I am a noob who fell deeply in love with your website, for the movie trailer, how can i possible make it autoplay without having to click the play button.
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:
 
Security Image
Please input the anti-spam code that you can read in the image.
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio