keyboard
11-21-2012, 08:48 AM
I've got a basic jquery version of one of the old ddscripts, and it works fine, but one of the bits of code is driving me crazy...
I don't want to write all the same code (most of it's the same) twice... is there a way to avoid this without making tonnes of variables to store all the data that changes between the if statements?
js file
$.fn.time_based_progress_bar = function(user_options) {
var default_options = {
duration : 1000,
color : 'blue',
direction : 'horizontal',
complete : function() {
//alert("Complete");
}
};
var args = default_options;
if(typeof user_options === 'object') {
args = $.extend(default_options,user_options);
}
if(default_options.direction == 'vertical') {
$(this).css('position', 'relative');
$(this).html('<div style="height:0%;width:100%;background-color:' + default_options.color + ';position:absolute;bottom:0;left:0;"></div>');
$(this).children().animate({
height : '100%'
}, default_options.duration, default_options.complete);
} else {
$(this).html('<div style="height:100%;width:0%;background-color:' + default_options.color + ';"></div>');
$(this).children().animate({
width : '100%'
}, default_options.duration, default_options.complete);
}
};
html
<!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="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.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',
complete : function() {
alert("Finished.");
}
});
});
</script>
</head>
<body>
<div id="time" style="border:1px solid black;background-color:white;height:20px;"></div>
</body>
</html>
p.s. Is it just me or is the bbcode color button missing?
I don't want to write all the same code (most of it's the same) twice... is there a way to avoid this without making tonnes of variables to store all the data that changes between the if statements?
js file
$.fn.time_based_progress_bar = function(user_options) {
var default_options = {
duration : 1000,
color : 'blue',
direction : 'horizontal',
complete : function() {
//alert("Complete");
}
};
var args = default_options;
if(typeof user_options === 'object') {
args = $.extend(default_options,user_options);
}
if(default_options.direction == 'vertical') {
$(this).css('position', 'relative');
$(this).html('<div style="height:0%;width:100%;background-color:' + default_options.color + ';position:absolute;bottom:0;left:0;"></div>');
$(this).children().animate({
height : '100%'
}, default_options.duration, default_options.complete);
} else {
$(this).html('<div style="height:100%;width:0%;background-color:' + default_options.color + ';"></div>');
$(this).children().animate({
width : '100%'
}, default_options.duration, default_options.complete);
}
};
html
<!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="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.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',
complete : function() {
alert("Finished.");
}
});
});
</script>
</head>
<body>
<div id="time" style="border:1px solid black;background-color:white;height:20px;"></div>
</body>
</html>
p.s. Is it just me or is the bbcode color button missing?