I was recently asked by a headhunter if I had written any jQuery plugins. I said "No, but I could probably have one written by time we get off of the phone". OK, maybe a little cocky on my part, but not too far off from the truth.
Here is my first jQuery plugin. It is a simple ProgressBar. You could use it for many different applications. One example would be to display to the user the percentage complete he or she was in filling out a survey.
Before I get into the code, let me explain a little bit of what you are seeing to the left. The top jProgressBar uses an image. The next one subtly changed it's bar color from blue to white as the percentage increases. An finally, the sine wave that you see below is made up of 20 jProgressBars oriented vertically and two sine waves that are 90 degrees out of phase.
Using jProgressBar
Using jProgressBar is easy. Simply setup a get a jQuery object then call jProgressBar. In the example below, any HTML elements with a class of percentBar will be turned onto jQuery jProgressBar objects and initialized to a 10 percent full bar.
- var bar = $(".percentBar").jProgressBar(10);
Later, if you would like to change the percentage, you can call the method setPercent on any jProgressBar object. For example, the jProgressBar objects created above will now display a 50 percent full bar.
- bar.setPercent(50);
You can also pass an optional second argument during the creation of your jProgressBar to specify settings. For example, let's say you wanted your bar to be 125px wide. You could do something like this:
- var bar = $(".percentBar").jProgressBar(0, {width:125, height:50});
Settings
Here is a list of all of the jProgressBar settings and their default values:
| width | width of the jProgressBar (default=100) |
| height | height of the jProgressBar (default=12) |
| orientation | 0=horizontal, 1=vertical (default=0) |
| border.width | width of the border (default=1) |
Style
The style of the jProgressBar is controlled through CSS. You can set border, background and bar sub selectors. Here is the CSS that I used on the top jProgressBar in my sample above. The border is set to a solid color of black, the background (the space not occupied by the bar) is set to white and the bar itself is made up of an image.
- .jProgressBar_Green .border {
- background-color: #000000;
- }
- .jProgressBar_Green .border .background {
- background-color: #ffffff;
- }
- .jProgressBar_Green .border .background .bar {
- background: #6dd436 url(images/jProgressBarBackground.png) right top;
- }
Get the code
jProgressBar is being offered on CodePlex. You can get the latest code here.
Labels: code, technology
1 comments:
- hong_hai_long said on November 19, 2010 3:47 AM ...
-
I've read some good stuff here. Definitely worth bookmarking for revisiting.
windows 7
Post a Comment