Skip to content

Instantly share code, notes, and snippets.

@ncherro
Created October 3, 2012 22:55
Show Gist options
  • Save ncherro/3830417 to your computer and use it in GitHub Desktop.
Save ncherro/3830417 to your computer and use it in GitHub Desktop.
Simple jquery plugin fades image in on load
(function($) {
var PrettyLoader = function(el, opts) {
var settings = $.extend({}, $.fn.prettyloader.defaults, opts)
$el = $(el);
function init() {
$el.hide().one('load', loaded).each(function() {
// manually call load() if the image has been cached
if (this.complete) $el.load();
});
}
function loaded() {
$el.fadeIn(settings.transition);
}
init();
};
$.fn.prettyloader = function(options) {
return this.each(function(idx, el) {
var $el = $(this), key = 'prettyloader';
if ($el.data(key)) { return; }
var prettyloader = new PrettyLoader(this, options);
$el.data(key, prettyloader);
});
};
$.fn.prettyloader.defaults = {
transition: 300
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment