Posted By

Leech on 07/21/06


Tagged

images preloader


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

panatlantica
jkochis
visualAesthetic
shachi
vali29
kele5240
seanpowell
guo3823538
wirenaught


Image Preloader v1.0


 / Published in: JavaScript
 

URL: http://jsfromhell.com/classes/preloader

Image preloader with events and allows grouping the images to supply a synchronized loading. Created: 2005.08.13

  1. /*
  2. **************************************
  3. * Preloader Class v1.0 *
  4. * Autor: Carlos R. L. Rodrigues *
  5. **************************************
  6. */
  7. Preloader = function(){
  8. var o = this;
  9. o.img = []; o.r = []; o.g = [];
  10. o.n = {}; o.total = o.loaded = 0;
  11. };
  12. Preloader.prototype.add = function(i, g){
  13. var o = this, n = (o.g[o.total] = [g || "general"])[0];
  14. (g = o.n)[n] >= 0 ? ++g[n] : g[n] = 1;
  15. o.img[o.total++] = i;
  16. };
  17. Preloader.prototype.load = function(){
  18. var o = this, p = o.img, l = p.length, a, g = o.g;
  19. while(l--){
  20. (g[l][1] = a = new Image()).src = p[a.i = l];
  21. a.onload = function(){
  22. if(o.r[this.i]) return;
  23. !--o.n[g[this.i][(o.r[this.i] = 1) - 1]] && o.onGroupComplete && o.onGroupComplete(g[this.i][0]);
  24. o.onImageComplete && o.onImageComplete(this);
  25. (++o.loaded == o.total) && o.onComplete && o.onComplete();
  26. };
  27. (a.fileSize !== undefined ? a.fileSize > -1 : a.width) && a.onload();
  28. a.onerror = function(){
  29. o.onImageError && o.onImageError(this);
  30. }
  31. }
  32. };
  33. Preloader.prototype.getImagesByGroup = function(n){
  34. var g = this.g, i = [], p = 0, n = n || "general";
  35. for(var j = g.length; j; g[--j][0] == n && (i[p++] = g[j][1]));
  36. return i;
  37. };

Report this snippet  

You need to login to post a comment.