Skip to content

Instantly share code, notes, and snippets.

@alepez
Created October 15, 2015 09:43
Show Gist options
  • Save alepez/60fe0276d832370b3192 to your computer and use it in GitHub Desktop.
Save alepez/60fe0276d832370b3192 to your computer and use it in GitHub Desktop.
$.fn.dt_smartPreloader = function (action) {
var args = arguments;
return this.each(function (index, self) {
var $self = $(self);
var visibility = $self.css('visibility');
/* wrap $self with a preloader div */
$self.wrap($('<div>').addClass('preloader-mask ajaxLoading'));
var $preloader = $self.parent();
var actions = {
start: function () {
/* hide the loading element */
$self.css('visibility', 'hidden');
},
stop: function () {
/* restore initial visibility */
$self.css('visibility', visibility);
/* unwrap the element */
$preloader.replaceWith($self);
},
load: function (url, callback) {
actions.start();
$preloader.load(url, function (data) {
actions.stop();
if (callback) callback($preloader.children());
});
}
};
actions[action].apply(this, Array.prototype.slice.call(args, 1));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment