YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: ProgressBar Control: Events

# ProgressBar Control: Events

This example shows how you can use the events that ProgressBar fires.

View example in new window.

0
255
127
Control
Value
Range

The code to create the ProgressBar is:

var pb = new YAHOO.widget.ProgressBar({ value:127, maxValue:255, anim:true }).render('pbContainer'); var anim = pb.get('anim'); anim.duration = 3; anim.method = YAHOO.util.Easing.bounceBoth;

We instantiate the ProgressBar and configure it so that the initial value is 127, which is halfway between 0 and 255. We chose this range so that the values can translate directly into color values, which we then apply to the bar. The ProgressBar uses an Animation instance which we can get and access directly to set its duration and easing method.

We are listening to the three events related to movement:

- `start` signals the start of the movement
- `progress` is fired during the movement
- `complete` signals the completion of the movement

Without animation, the `start`, `progress` and `complete` events fire one after the other in close sequence.

pb.on('start',function (value) { Dom.get('status').innerHTML = 'Started'; window.setTimeout(function() { Dom.get('status').innerHTML = ''; },1000); }); pb.on('progress',function (value) { Dom.setStyle(this.get('barEl'),'backgroundColor','rgb(' + value + ',' + value + ',127)'); Dom.get('valueContainer').innerHTML = value; }); pb.on('complete',function (value) { Dom.get('status').innerHTML = 'Stopped'; window.setTimeout(function() { Dom.get('status').innerHTML = ''; },1000); });

Our event handlers do a few simple things:

- we display 'Started' for one second upon receiving the `start` event
- we change the color of the bar itself to reflect the `value` received from the `progress` event
- we show the current value right below the bar (all events provide the `value`)
- we display 'Stopped' upon receiving the `complete` event

The `value` argument passed by all events provides the nominal value of the ProgresBar, not the size of the bar itself. A value of 50 will always be halfway in a range of 0 to 100, whether the ProgressBar is 100 or 500 pixels wide.

We have also provided a slider to change the end values for the bar so that we can show how to respond to those events and display their values. The ProgressBar inherits the attribute change events from the Element Utility, so before and after change events are available for all configuration attributes.

pb.on('minValueChange',function(oArgs) { Dom.get('minValueContainer').innerHTML = oArgs.newValue; }); pb.on('maxValueChange',function(oArgs) { Dom.get('maxValueContainer').innerHTML = oArgs.newValue; });

The markup for the text labels are shown below. Notice the `yui-pb-range` and `yui-pb-caption` class names in their declaration. The ProgressBar itself does not use those class names at all, but they are defined as part of the "Sam" skin as a convenience to the implementer.

<div id="textContainers"> <div id="minValueContainer" class="yui-pb-range">0</div> <div id="maxValueContainer" class="yui-pb-range">255</div> <div class="yui-pb-caption"> <span id="valueContainer">127</span> <span id="status"></span> </div> </div>

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### ProgressBar Control Examples:

### More ProgressBar Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings