Tuesday, 9 July 2013

Making a jQuery banner from scratch

Woah, hang on a minute...  You mean you want me to build my own jQuery solution from scratch!?  Why would I do that when there are so many pre-packaged jQuery solutions out there?

Good question, if you are only making something very simple, maybe you'd like to optimise the amount of code the browser has to execute, and the pre-packaged solution may have many more features than you need...  Ok, I'll admit that's a bit far fetched for me to use as my own excuse.  I had some spare time, and wanted to see how easy it was.

The answer, as always, is it depends.

I was migrating a website from the early 2000's to a SharePoint site and I just wanted to replace a simple flash banner that faded in/out images & animated text.  Great little project I thought.

Here's how:
First I made a simple HTML page with a div for every image and sentence I wanted to fade in and out.  and an id against every div, so that I could reference them in the jQuery.

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" />
</head>
<body>
<div id="background">
<div id="sydney"><div id="busy">Busy Joint</div></div>
<div id="melbourne"><div id="cold">Cold Joint</div></div>
<div id="brisbane"><div id="small">Small Joint</div></div>
<div id="perth"><div id="exxy">Exxy Joint</div></div>
</div>
</body>

I used CSS to hide all div's by default using 'display: none'.  and of course, used CSS to set all the other look/feel goodies.

Now to start Querying the J (jQuerying... ahem.):

A few pointers.

  • All jQuery needs to be inside <script> tags
  • Anything inside the Document Ready Function will run when the page loads
  • If you wish to cycle your animation indefinitely, my favourite way is to put the animation inside it's own function, which calls itself on the last line of the function.
  • Whenever you want to do something to an item, you call it like this: $("#[css id]").[dosomething]()   So the jQuery Syntax is: $(selector).action() 

Here's the beginning of my jQuery, fading in my 'sydney' and 'busy' divs, then animating the 'busy' words and then fading them out again:

<script>    
$(document).ready(function(){  
  
    $("#sydney").fadeIn("slow");    
    $("#busy").fadeIn("slow");        
    $("#busy").animate({left:"150px"}, 700, function(){     
        $("#sydney").fadeOut("slow");    
        $("#busy").fadeOut("slow");  
    });

});
</script>

Notice on the animate step, I've called a function which runs the FadeOut steps.  This is because although jQuery runs step by step, it doesn't wait until one step is finished to run the next step unless you tell it to.  So what I'm saying here is, don't fade the suckers out until the animation has completed.

So I wanted to make the banner do this four times with different DIV's.  All you need to do is rinse and repeat really!  Yes I probably could have optimised the code even further, but this is a simplistic version in order to teach a beginner how it works:

<script>
$(document).ready(function(){


startAnimation();

function startAnimation(){    
    $("#sydney").fadeIn("slow");    
    $("#busy").fadeIn("slow");        
    $("#busy").animate({left:"150px"}, 700, function(){     
    $("#sydney").fadeOut("slow");    
    $("#busy").fadeOut("slow");  

    $("#melbourne").delay(1000).fadeIn("slow");    
    $("#cold").delay(1000).fadeIn("slow");        
    $("#cold").animate({right:"150px"}, 700, function(){     
    $("#melbourne").fadeOut("slow");    
    $("#cold").fadeOut("slow");  

    $("#brisbane").delay(1000).fadeIn("slow");    
    $("#small").delay(1000).fadeIn("slow");        
    $("#small").animate({left:"150px"}, 700, function(){     
    $("#brisbane").fadeOut("slow");    
    $("#small").fadeOut("slow");  

    $("#perth").delay(1000).fadeIn("slow");    
    $("#exxy").delay(1000).fadeIn("slow");        
    $("#exxy").animate({right:"150px"}, 700, function(){     
    $("#perth").fadeOut("slow");    
    $("#exxy").delay(1000).fadeOut("slow",startAnimation);  
    });
   });
 });
});
}
});
</script>

I added a little bit extra there..
  • Used the Delay Action to make the steps wait 1 second.
  • Inserted a function to run all the animations, so that I could get the function to call itself and run forever.
  • The animate settings is telling the CSS to move the div right and left.
Hopefully that's not too much info.  Comment if you have any questions.

No comments:

Post a Comment