Skip to content

Commit

Permalink
Updated loadie.js script
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Schmidt committed Nov 13, 2013
1 parent ade8179 commit 13188b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
44 changes: 29 additions & 15 deletions js/jquery.loadie.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,60 @@
/**
* Loadie.js by 9elements (@sippndipp & @iDuuck)
*/
(function( $ ) {
function uniqueId() {
var Loadie = {};

/*
* Generate a unique id for more than one loadie
*/
Loadie.uid = function() {
var newDate = new Date;
return newDate.getTime();
}
};

function destroyGUI(dom) {
/*
* Finishes and fades the loadie out.
*/
Loadie.finish = function(dom) {
var loadie = $('#loadie-' + dom.data('loadie-id'), dom);
loadie.fadeOut(200);
}

function updateGUI(dom, percent) {
/*
* Updates loadie with a float
*
* Loadie.update(0.2)
* Loadie.update(1) // Finishes loadie, too
*/
Loadie.update = function(dom, percent) {
var loadie = $('#loadie-' + dom.data('loadie-id'), dom);
var parentWidth = dom.width();
loadie.css('width', Math.floor(percent * parentWidth) + "px");
}

function createGUI(dom, percent) {
var uid = uniqueId();
/*
* Loadie.js initializer
*/
Loadie.init = function(dom, percent) {
var uid = this.uid();
var loadie = dom.append($('<div id="loadie-' + uid + '" class="loadie"></div>'));
dom.data('loadie-id', uid);
dom.css('position', 'relative');
updateGUI(dom, percent);
this.update(dom, percent);
}

$.fn.loadie = function(percent, callback) {
var percent = percent || 0;
var parent = $(this);

if(parent.data('loadie-loaded') !== 1) {
createGUI(parent, percent);
Loadie.init(parent, percent);
} else {
updateGUI(parent, percent);
Loadie.update(parent, percent);
}
if(percent >= 1) {
setTimeout(function() {
destroyGUI(parent);
}, 700);
Loadie.finish(parent);
}, 200);
}
parent.data('loadie-loaded', 1);
return this;
};
}( jQuery ))
}( jQuery ))
2 changes: 1 addition & 1 deletion js/jquery.loadie.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 13188b1

Please sign in to comment.