I did add one variable ($bar), which seemed prudent. You don't need the UI because you're not using anything from it. The animation function can take an options object and doesn't seem to mind the extra/unrecognized options in your existing object. So you don't have to parse it each time. I added an easing option. If you want more than 'linear' or 'swing' as easing choices, then you would need the UI or at least an extended easing function. The latter exist or you could lift it from an uncompressed version of the UI. The this keyword in a jQuery fn function is already a jQuery object. Chaining can be utilized more. An if statement that has only one action can be done more succinctly as an && statement. An fn function should return the jQuery object it was run on, The appendTo() function is safer than the .html() one. If you want to make sure the parent is empty, you can use the empty() function on it, but since it is empty, I didn't bother. Using the css() function is often more succinct than creating an element as a string with a style attribute in it:
Code:
$.fn.time_based_progress_bar = function(user_options) {
var default_options = {
duration : 1000,
color : 'blue',
direction : 'horizontal',
easing: 'swing',
complete : function() {
//alert("Complete");
}
}, $bar = $('<div />');
typeof user_options === 'object' && $.extend(default_options, user_options);
$bar.css({backgroundColor: default_options.color});
if(default_options.direction == 'vertical') {
$bar.css({height: 0, width: '100%', position: 'absolute', bottom: 0, left: 0})
.appendTo(this.css('position', 'relative')).animate({height : '100%'}, default_options);
} else {
$bar.css({height: '100%', width: 0}).appendTo(this).animate({width : '100%'}, default_options);
}
return this;
};
HTML Code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="time_based.js"></script>
<script type="text/javascript">
$(function() {
$('#time').time_based_progress_bar({
duration : 5000,
color : 'green',
direction : 'vertical',
easing: 'linear', // 'linear' or 'swing' defaults to 'swing'
complete : function() {
alert("Finished.");
}
});
});
</script>
</head>
<body>
<div id="time" style="border:1px solid black;background-color:white;height:20px;"></div>
</body>
</html>
BTW - The color option in the editor is there for me. It's a button designated by a capital A. It has no effect if used within HTML or any other bbcode code block other than [code].
Edit: An even more concise way of doing the javascript:
Code:
$.fn.time_based_progress_bar = function(user_options) {
var default_options = {
duration : 1000,
color : 'blue',
direction : 'horizontal',
easing: 'swing',
complete : function() { /* alert("Complete"); */ }
}, $bar = $('<div />'), vert;
typeof user_options === 'object' && $.extend(default_options, user_options);
vert = default_options.direction === 'vertical';
$bar.css($.extend({backgroundColor: default_options.color, width: 0, height: 0},
(vert? {width: '100%', position: 'absolute', bottom: 0, left: 0} : {height: '100%'})));
return this.empty().append($bar.animate((vert? {height: '100%'} : {width: '100%'}), default_options)).css('position', 'relative');
};
Bookmarks