Today we’d like to share an interesting progress button effect with you. The effect is based on a very nice Dribbble shot called “Download” by xjw. The button starts as an icon with an arrow and once it’s clicked, it animates into a fun little wire and a label that indicates the download percentage.
Here you can learn to creates a button that turns into a progress bar with a elastic effect.
Take a look:
This project requires
GSAP. You can use either TweenMax...
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/TweenMax.min.js"></script>
...or TweenLite, with EasePack and the CSS and attr plugins:
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/TweenLite.min.js"></script>
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/easing/EasePack.min.js"></script>
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/plugins/CSSPlugin.min.js"></script>
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/plugins/AttrPlugin.min.js"></script>
Then, include the elastic-progress.min.js file, located in the dist folder:
<scriptsrc="path/to/js/elastic-progress.min.js"></script>
How to implements
Create the element you want to turn into a button:
<divclass="Upload"role="button"aria-label="Upload file"></div>
Note: We are using a div element with role="button" instead of a button element because, according to W3C recommendation, button elements should have no interactive elements as descendants.
Then, via JS:
var element=document.querySelector('.Upload');
var progress=new ElasticProgress(element, { /*options*/ });
// or...
var progress=new ElasticProgress('.Upload', { /*options*/});
Or, in case you are using jQuery:
$('.Upload').ElasticProgress({/*options*/});
Setting Options
Options are set on the constructor, like this:
var progress=new ElasticProgress('.Upload', {
colorFg:"#FF0000",
buttonSize:80,
//...
})
A complete list of options can be found below.
Calling Methods
The button doesn't do much by itself - controlling the opening, bar progress, etc. is in your charge.
var progress=new ElasticProgress('.Upload', {
// ...
onClick:function(){
progress.open();
}
});
function theFunctionYouAreUsingToCheckProgress(){
// ...
progress.setValue(value);
}
// with jQuery
$(".Upload").ElasticProgress({
// ...
onClick:function(){
$(this).ElasticProgress('open');
}
});
function theFunctionYouAreUsingToCheckProgress(){
// ...
$(".Upload").ElasticProgress('setValue',value);
}
A complete list of methods can be found below.
Options
Either 'up' or 'down'. Defaults to 'down'.
Colors
Colors of the foreground (the arrow, the filled part of the progress bar) and the background (the circle, the empty part of the progress bar), respectively. Defaults are white and black.
Color of the highlight outline. Defaults to #08F.
Color of the overlay during the "pop" animation. Defaults to the background color of the body.
Size
Circumference of the button. Defaults to the height of the element.
Width of the expanded progress bar. Defaults to the width of the element.
Height of the label, in pixels. Defaults to 53.
Thickness of the progress bar. Defaults to 4.
Inset of the filled part of the progress bar. Defaults to -0.5 Helps covering jagged edges.
- bleedTop, bleedRight, bleedLeft and bleedBottom number
Margin to draw the graphics. If there's clipping during the animation, increase these values. Performance might take a hit for values too large. Defaults to 100, 50, 50 and 60 respectively.
Text
Font used for the label. Defaults to 'Helvetica Neue','Helvetica','Arial',sans-serif. This default is added to the value set, so there's no need to manually set these as fallback.
Defaults to 'bold'.
- textComplete, textFail and textCancel string
Texts that will be shown on these events. Defaults are 'Done', 'Failed' and 'Canceled'.
Animation
The maximum distance the bar will stretch. Defaults to 20.
How hight the arrow/label will jump. Defaults to 50.
- barElasticOvershoot and barElasticPeriod number
Settings for the elastic animation. Defaults are 1.8 and 0.15, respectively.
Setting for the animation of the label during progress. Defaults to 40.
- arrowHangOnFail and arrowHangOnCancel boolean
Whether the arrow should 'fall' on these events or not. Default is true for both.
Events
Called when the user clicks the button only.
Called when the progress bar finishes the opening animation.
Called when the bar value is changed.
Called when the bar is full.
Called when the close animation is finished.
Called when the fail animation starts.
Called when the cancel animation starts.
Methods
Starts the opening animation (turns the button into a progress bar).
Turns the progress bar back into a button.
Sets the percentage loaded of the progress bar. From 0 to 1.
Returns the current value of the progress bar.
Runs the fail and the cancel animations, respectively.
Runs the complete animation, regardless of the progress. You should probably call setValue(1) instead.
- onClick(callbackfunction), onOpen(callbackfunction), onChange(callbackfunction), onComplete(callbackfunction), onClose(callbackfunction), onFail(callbackfunction) and onCancel(callbackfunction)
Aliases to the options of the same name.
We hope you enjoyed this progress button and find it useful!