Created
December 19, 2012 09:45
-
-
Save arkadylukashov/4335606 to your computer and use it in GitHub Desktop.
Simple image preloader uses jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.preload = function(forPreload, options) { | |
var settings = $.extend({ | |
debug : false, | |
percentage : { | |
textCnt : '', | |
lineCnt : '' | |
}, | |
onBuild: $.noop, | |
onComplete : $.noop | |
}, options); | |
var items = new Array(), | |
errors = new Array(), | |
success = new Array(), | |
current = 0, | |
root = this, | |
// all methods | |
methods = { | |
run : function() { | |
settings.onBuild(); | |
// if no items, please exit.. | |
if (!forPreload || forPreload.length == 0) | |
return methods.loadComplete(); | |
items = forPreload; | |
// go | |
for (var i = 0; i < items.length; i++) { | |
methods.loadImg(items[i]); | |
} | |
}, | |
loadImg : function(url) { | |
// loading... | |
var imgLoad = new Image(); | |
$(imgLoad) | |
.load(function() { | |
success.push($(this).attr('src')) | |
methods.completeLoading(); | |
}) | |
.error(function() { | |
errors.push($(this).attr('src')); | |
methods.completeLoading(); | |
}) | |
.attr('src', url); | |
}, | |
completeLoading : function() { | |
current++; | |
// get current percentage | |
var percent = Math.round((current / items.length) * 100); | |
// update percents | |
methods.updatePercents(percent); | |
// stop or continue | |
if(current >= items.length) { | |
current = items.length; | |
if (settings.debug) { | |
methods.debug(); | |
} | |
methods.loadComplete(); | |
} | |
}, | |
loadComplete: function() { | |
methods.updatePercents(100, settings.onComplete.call(root)) | |
}, | |
updatePercents: function(percent, callback) { | |
$(settings.percentage.lineCnt) | |
.stop() | |
.animate( | |
{ | |
width: percent + '%' | |
}, | |
100, | |
function() { | |
if (callback) callback() | |
} | |
); | |
$(settings.percentage.textCnt).text(percent+"%"); | |
}, | |
debug: function() { | |
if (!$.browser.msie) { | |
console.log('Preloader Debugger\n=================='); | |
// success | |
console.groupCollapsed('Successful load',success.length,'images') | |
if (success.length==0) { | |
console.log('Global fail... =('); | |
} else { | |
$.each(success, function(i,v) { | |
console.log(v) | |
}) | |
} | |
console.groupEnd(); | |
// errors | |
console.group('Error load',errors.length,'images') | |
if (errors.length==0) { | |
console.log('No errors'); | |
} else { | |
$.each(errors, function(i,v) { | |
console.log(v) | |
}) | |
} | |
console.groupEnd(); | |
} | |
} | |
} | |
methods.run(); | |
return this; | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){$.fn.preload=function(forPreload,options){var settings=$.extend({debug:false,percentage:{textCnt:"",lineCnt:""},onBuild:$.noop,onComplete:$.noop},options);var items=new Array,errors=new Array,success=new Array,current=0,root=this,methods={run:function(){settings.onBuild();if(!forPreload||forPreload.length==0)return methods.loadComplete();items=forPreload;for(var i=0;i<items.length;i++)methods.loadImg(items[i])},loadImg:function(url){var imgLoad=new Image;$(imgLoad).load(function(){success.push($(this).attr("src"));methods.completeLoading()}).error(function(){errors.push($(this).attr("src"));methods.completeLoading()}).attr("src",url)},completeLoading:function(){current++;var percent=Math.round(current/items.length*100);methods.updatePercents(percent);if(current>=items.length){current=items.length;if(settings.debug)methods.debug();methods.loadComplete()}},loadComplete:function(){methods.updatePercents(100,settings.onComplete.call(root))},updatePercents:function(percent,callback){$(settings.percentage.lineCnt).stop().animate({width:percent+"%"},100,function(){if(callback)callback()});$(settings.percentage.textCnt).text(percent+"%")},debug:function(){if(!$.browser.msie){console.log("Preloader Debugger\n==================");console.groupCollapsed("Successful load",success.length,"images");if(success.length==0)console.log("Global fail... =(");else $.each(success,function(i,v){console.log(v)});console.groupEnd();console.group("Error load",errors.length,"images");if(errors.length==0)console.log("No errors");else $.each(errors,function(i,v){console.log(v)});console.groupEnd()}}};methods.run();return this}})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment