Home  >  

Building a Simple Progress Bar Using the Scriptaculous Morph Function

Author photo
| | Comments (5)
AddThis Social Bookmark Button

One of the most useful script.aculo.us functions has to be the morph function. You can use it to modify a CSS style over a set period of time, making it the perfect function for creating an animated progress bar for a JavaScript based RIA.

Below is a quick example showing how to use the morph function.

In this example I begin by creating a couple of quick graphics. The first is for the progress bar and a the second is transparent gif to act as a border and a masking agent for the progress bar. The gif image is not necessary, but it does give us a quick and easy way to create rounded corners for the progress bar.

After the graphics are saved, I create a simple html page with a div for the progress bar. I'll add some CSS to style the progress bar div as follows:

.progressBar
{
    width: 480px;
    height: 30px;
    background: url(progressBar.png) left no-repeat;
    padding: 0;
    margin: 0;
}

I then add a javascript reference to prototype.js and effects.js from scriptaculous.

Next, I add a dom:loaded event handler to initialize my progressbar. I also use the event to add the progress bar mask image. Then I set the background position to -480px (the length of the div). Finally I use the morph function to change the background position to 0px over a duration of 4 seconds.

below is the code:


document.observe("dom:loaded",function() {
var pb = $('sampleProgressBar');
pb.insert("<img src=\"progressBarMask.gif\" alt=\"progress bar\" />");
pb.setStyle({backgroundPosition: '-480px'});
pb.morph({backgroundPosition: '0px'}, { duration: 4 });
});

Read more from Jeremy Bierly. Jeremy Bierly's Atom feed

Comments

5 Comments

John Rudy said:

Awesome technique ... Looking forward to reading more of these as I try to get into the RIA world from my too-traditional browser and desktop GUI world.

Lawrence said:

Well done Mr. Bierly.

Using an image mask and background image moved via JS can provide a variety of effects.

You didn't mention why the background image is positioned out of sight using JS and not CSS.

Dave said:

Do you have a sample code combine in zip file? So that we can download it. It doesnt work for me.. Thank you...

nils said:

.progressBar
{
border:1px solid grey;
width: 480px;
height: 30px;
padding: 0;
margin: 0;
}


javascript:

document.observe("dom:loaded",function() {

var pb = $('sampleProgressBar');

pb.setStyle({background: 'progressBar.gif'});
pb.setStyle({backgroundPosition: '-480px'});

pb.morph({backgroundPosition: '0px'}, { duration: 4 });

});

nils said:

<div id="sampleProgressBar" class="progressBar">

</div>

Leave a comment


Type the characters you see in the picture above.

Tag Cloud

Poll: Sci-Fi Movies

What's Your Favorite Sci-Fi Movie of All Time?

Vote | View Poll Results | Read Related Blog Entry

Latest Features

  •     Welcome back to the series. This time we are goings to build a really exciting component that will be used to simply display information about the user. Well, you might say why to we need such a component, is there... Continue Reading
  •    Welcome back to our exciting Facebook ActionScript series. In this article we will discuss one of most important (and most exciting) features of the FB platform, it's the publishing of news. We all know when we log in to facebook,... Continue Reading
  • This article provides 10 tips and best practices (in no particular order) for maximizing the benefits that Dojo can bring to your next project. For a more thorough introduction to Dojo, see the article Dojo: The JavaScript Toolkit with... Continue Reading
  •     The notifications are one of the most interesting (and important) parts of the facebook area. In order to completely understand the Flash side of it, we need to understand the basics of the facebook notification, what it is and how... Continue Reading

Development Series

Get an overview of the tools and technologies that work together to allow developers to build Rich Internet Applications (RIAs) quickly and easily.

facebook icon Facebook Application Development

Anatomy of an Enterprise Flex RIA

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.