Preload images using jquery

jQuery Image Preloader. Basically it is a script that loads images in the background and track number of loaded images. It’s very simple, only 25 lines of code, but maybe useful if you are creating jQuery image gallery or some other script that requires image preloading.

Lets start of with how it works. As i understand it, it’s not actually a plugin but rather jQuery extension. It means that it’s not applied to HTML elements but rather used as function. Here’s how you can use it.

(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;

			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}

		}
	});
})(jQuery);

$(function() {
    $.preload([
        "http://mytestsite.com/img/banner1.jpg",
        "http://mytestsite.com/img/banner2.jpg",
		"http://mytestsite.com/img/banner3.jpg",
		"http://mytestsite.com/img/banner4.jpg",
		"http://mytestsite.com/img/banner5.jpg",
		"http://mytestsite.com/img/banner6.jpg",
		"http://mytestsite.com/img/banner7.jpg",
		"http://mytestsite.com/img/banner8.jpg",
		"http://mytestsite.com/img/banner9.jpg",
		"http://mytestsite.com/img/banner10.jpg",
		"http://mytestsite.com/img/banner11.jpg",
		"http://mytestsite.com/img/banner12.jpg"
    ], {
        init: function(loaded, total) {
           // $("#indicator").html("Loaded: "+loaded+"/"+total);
        },
        loaded: function(img, loaded, total) {
            //$("#indicator").html("Loaded: "+loaded+"/"+total);
            //$("#full-screen").append(img);
        },
        loaded_all: function(loaded, total) {
           // $("#indicator").html("Loaded: "+loaded+"/"+total+". Done!");
        }
    });
});

$.preload() function takes two parameters. First is an array of urls that will be loaded. Second is an object that can have up to three callbacks:

–> init() – executed before the first loading images start
–> loaded() – executed when image is loaded (thus if there are 10 images then this callback is executed 10 times, for each image once)
–> loaded_all() – executed when all images are loaded

Share This Post

Related Articles

126 Responses to “Preload images using jquery”


  1. Hé c’est un grand poteau. Est-ce que je peux employer une partie là-dessus sur mon emplacement ? Je naturellement lierais à votre emplacement ainsi les gens pourraient lire le plein article s’ils voulaient à. Remercie l’une ou l’autre manière.

  2. areola says:

    No BS and well written, thank you for the info

  3. hostclear says:

    Me too, tyvm for sharing this..


  4. I just added your website to my bookmarks. I like reading your posts. Tyvm!


  5. Thank you for blogging this, it was quite handy and told me quite a bit


  6. TY a ton for putting this up, it was unbelieveably informative and showed me a lot


  7. When I open up your RSS feed it throws up a whole lot of weird text, is the deal on my side?

  8. homepage says:

    No BS and written well, I appreciate for the post


  9. When I click on your RSS feed it looks like a bunch of strange characters, is the deal on my side?


  10. I’m having a strange problem I cannot get my reader to pickup your feed, I’m using google reader by the way.


  11. I’ve meant to post about something like this on my webpage and this has given me an idea. Thanks.


  12. Can I put part of this on my blog if I post a link back to this webpage?


  13. Thought I would comment and say neat theme, did you make it yourself? Really looks really good!


  14. I agree, thanks for posting this..

  15. t-shirt says:

    I have wanted to write about something like this on one of my blogs and this has given me an idea. Thank you.


  16. Thought I would comment and say neat theme, did you create it for yourself? It’s really awesome!

  17. vizio lcd says:

    When I click your RSS feed it gives me a page of garbage, is the malfunction on my side?


  18. TY a ton for posting this, it was very handy and helped very much


  19. Thanks a lot for posting this, it was very handy and told me immensely


  20. Is it alright to put some of this on my blog if I include a link back to this site?


  21. I just added your feed to my bookmarks. I really enjoy reading your posts. Ty!


  22. When I click on your RSS feed it gives me a whole lot of strange characters, is the deal on my end?


  23. It absolutely was about time that My spouse and i finally read some thing worthwile. Carry on the great work!


  24. TY for blogging this, it was quite handy and showed me tons


  25. When I open your RSS feed it seems to be a page of weird text, is the malfunction on my side?


  26. I’m having a weird issue I cannot get my reader to pickup your rss feed, I’m using google reader fyi.


  27. Thanks a lot for posting, it was very handy and told tons

  28. Drum God says:

    Thanks a ton for writing this, it was quite helpful and showed me a lot


  29. Tyvm for the useful post! I would never have discovered this otherwise!

  30. Car Cover says:

    I just added this feed to my bookmarks. I like reading your posts. Ty!


  31. I agree, thanks for sharing this..

  32. fitness says:

    Just thought I would comment and say neat theme, did you code it for yourself? It looks awesome!


  33. You made a number of good points there. I did a search on the theme and found nearly all persons will consent with your blog.


  34. Is it okay to post some of this on my page if I include a link back to this site?


  35. Thank you for the helpful post! I would never have discovered this by myself!


  36. You made some good points there. I searched this topic and found out that many people will agree with your web site.


  37. No BS and written well, thank you for the post


  38. i am leaving for the office and bookmarked it and sent to my office email will try to take the time out in the office to read it ..

  39. bmw says:

    This really answered my problem, thank you!


  40. Just thought I would comment and say neat theme, did you code it on your own? It looks really good!


  41. Thank you for the great information! I would not have found this on my own!


  42. I’ve meant to post about something like this on my site and you have given me an idea. TY.


  43. Straight to the point and written well, thanks for the information


  44. Is it okay to reference part of this on my blog if I post a link back to this webpage?


  45. Straightforward and well written, thanks much for the post


  46. Me too, thanks for posting this..


  47. You made a few good points there. I did a search on the issue and found mainly persons will agree with your blog.


  48. I’m having a small problem I can’t get my reader to pickup your feed, I’m using google reader fyi.


  49. Hi, I came via google to here and also to be sincere i liked these content articles, very informative and easy to study. Thank you SO much for this, it hard to discover some good content articles in web at nowadays. Greatest Regards


  50. thanks amigo! great post!


  51. nice share , 10x for this admin , i will boockmark your site because i’vde found interesting informations here!


  52. Noticed your web page on del.icio.us today and truly loved it.. i saved it and will be back to check it out some more later .. As a Noob, I am frequently seeking online for posts that can help me. Best wishes!


  53. I really loved reading your blog. It was very well written and simple to undertand. Unlike additional blogs I have read. I also found it very interesting. In fact after reading, I had to go show the spouse and she ejoyed it as well!


  54. This really is this kind of a fantastic resource that you are offering and also you give it away free of charge. I adore seeing internet websites that understand the worth of offering a top quality useful resource free of charge. It?s the old what goes around arrives around program.


  55. I’m having a tiny problem I cannot seem to be able to subscribe your feed, I’m using google reader fyi.


  56. Cool story it is without doubt. My teacher has been searching for this tips


  57. It only goes to show where there’s will there’s a way. Keep on trying. – I have bad reflexes. I was once run over by a car being pushed by two guys. – Woody Allen Born 1935


  58. Outstanding post it is definitely. We’ve been seeking for this content


  59. It had been about time that I finally read some thing worthwile. Keep up your great work!


  60. Good post! I think that there’s so many things that can be applied or compared to this in our everyday lives.


  61. Exciting new post. Even better compared to your prior ones IMHO ;)


  62. That’s an interesting perspective. We were just talking about this the other night. I kind of agree on some things, but not all. Maybe a neutral pov should be considered.

  63. Belicia says:

    Christ Would be the Person

  64. jocuri says:

    Nice share admin , this is a interesting blog u have here

  65. Teen Tube says:

    Really instructive and good structure of articles , now that’s user genial


  66. Hmm, well written i would say but a bit more information is needed :) still thanks for the effort


  67. Hé c’est un grand poteau. Est-ce que je peux employer une partie là-dessus sur mon emplacement ? Je naturellement lierais à votre emplacement ainsi les gens pourraient lire le plein article s’ils voulaient à. Remercie l’une ou l’autre manière.


  68. hello autohor, this article is well written, thanks for the effort


  69. This really is such a wonderful useful resource that you’re providing and you give it absent free of charge. I adore seeing web sites that understand the worth of providing a top quality useful resource for free. It?s the old what goes around comes about routine.


  70. Your blog has been the highlight of my day colon cleanse


  71. Sometimes I just think that people write and dont really have much to say. Not so here

  72. hair fall says:

    well well… who knows what else will people comment


  73. Fantastische Webseite. Sehr guter Post Gruss McGiver23


  74. You completed several nice points there. I did a search on the subject matter and found mainly persons will agree with your blog.


  75. keep up the great work , I read few blog posts on this website and I conceive that your web site is very interesting and contains sets of excellent information.


  76. keep up the great work , I read few content on this site and I think that your website is really interesting and contains sets of great information.

  77. fat cow says:

    Content is very nice


  78. Exactly how would you come up with all of this? You will need to have some really good basis about this matter correct?


  79. Perhaps a bit of a unusual respond, but My partner and i like your theme you use and My partner and i definitely wanted to inform you! Along with quite a few pleasant content this website is really very good :)

  80. Porn says:

    I envy your piece of work, thanks for all the great articles .


  81. Its Pleasure to understand your blog.The above articles is pretty extraordinary, and I really enjoyed reading your blog and points that you expressed. I really like to appear back over a typical basis,post a lot more within the topic.Thanks for sharing…keep writing!!!

  82. Cyrus says:

    Do you do guest posting? I imagine it’d be great for you to complete a guest spot on my web site, let me know if you’re game.


  83. immense log you obtain

  84. Speed says:

    It really is our requirement as Christians that can help occur the expression of Christ Christ


  85. Top notch post. I learn a new challenge on different blogs everyday. Few stimulating to learn to read content from other writers and be taught a little something from them. I’d like to use some of it content on my blog store mind. Natually I’ll gives a link into your web blog. Thanks sharing.


  86. Thank you so much for this! I haven’t been this moved by a blog for quite some time! You have got it, whatever that means in blogging. Anyway, You’re certainly someone that has something to say that people should hear. Keep up the wonderful job. Keep on inspiring the people!

  87. mutuelle says:

    This is really a extremely beneficial read for me, Have to admit you might be 1 in the most effective bloggers I ever saw.Thanks for posting this informative article.


  88. This can be a really good learn for me, Must admit that you are the most effective bloggers I ever saw.Thanks for posting this informative article.

  89. mutuelle says:

    I found your weblog on google and check several of the early posts. Preserve up the excellent operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading through additional from you later on!…


  90. You made some nice points there. I did a search on the issue and found most individuals will consent with your website.


  91. It is unusual for me to discover something on the internet that’s as entertaining and intriguing as what you’ve got here. Your page is lovely, your graphics are great, and what’s more, you use source that are relevant to what you’re talking about. You’re definitely one in a million, well done!


  92. I feel one of your current advertisements caused my internet browser to resize, you may well want to get that on your blacklist.


  93. May i use some of your information on my blog?

  94. Annuaire says:

    This is really a extremely beneficial read for me, Have to admit you might be 1 in the most effective bloggers I ever saw.Thanks for posting this informative article.


  95. I’m getting a javascript error, is anyone else?


  96. A great article, I just given this onto a university student who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him. :) .. So let me rephrase that: Thanks for the treat! But yeah Thnx for taking the time to talk about this, I feel strongly about it and love learning more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blogpost!


  97. I admire your work , thanks for all the great articles .

  98. mutuelle says:

    I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!…

  99. mutuelle says:

    Fantastic article! I’ll subscribe correct now wth my feedreader software package!…


  100. After study few of the blog posts on your blog now, and I truly like your style of blogging. I added it to my favorites website list and will be checking back soon. Pls check out my site also and let me know your opinion.


  101. This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.


  102. I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!…


  103. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! I’m sure you had fun writing this article.


  104. ¡Gran poste! Gracias por tardar la época de escribir algo que está realmente digno de la lectura. Encuentro demasiado a menudo el Info inútil y no algo que es realmente relevante. Gracias por su trabajo duro.


  105. Your place is valueble for me. Thanks!…


  106. Good post. I learn something totally new on different blogs everyday. It’s always stimulating to read simple things content from other writers and become familiar with a little something their own. I’d adore to apply certain from the content on my blog should you don’t mind. Natually I’ll supplies a link oh no- your websites. Thanks sharing.


  107. I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!…


  108. There is noticeably a bunch to identify about this. I suppose you made certain good points in features also.


  109. You are not the common blog writer, man. You certainly have something powerful to contribute to the net. Such a wonderful blog. I will return for more.


  110. I cling on to listening to the news lecture about receiving free online grant applications so I have been looking around for the best site to get one. Could you tell me please, where could i find some?

  111. Gay Video says:

    Pretty insightful post. Never believed that it was this simple after all. I had spent a beneficial deal of my time looking for someone to explain this subject clearly and you’re the only one that ever did that. Kudos to you! Keep it up


  112. I want to thankx for the time you have made in writing this article. I am hoping the same high-grade post from you in the future as well. In fact your creative writing skill has inspired me to begin my own blog now. Truly the blogging is spreading its wings rapidly. Your write up is a fine model of it.


  113. WONDERFUL Post.thanks for share..more wait .. ;)


  114. Typically I do not publish on blogs, but I would like to say that this put up definitely compelled me to do so! definitely wonderful put up


  115. Your place is valueble for me. Thanks!…


  116. Excellent brief and this article helped me alot. Say thank you I looking for your information….

  117. mutuelle says:

    Hi! Your article rocks and is really a very good understand!…


  118. Very well written article. It will be supportive to anyone who utilizes it, as well as yours truly :) . Keep up the good work – for sure i will check out more posts.

  119. mutuelle says:

    This website is awesome. I constantly come across something new & different right here. Thank you for that data.

  120. vlc says:

    I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!…

  121. seotons says:

    This is really a extremely beneficial read for me, Have to admit you might be 1 in the most effective bloggers I ever saw.Thanks for posting this informative article.

  122. news says:

    I found your entry interesting do I’ve added a Trackback to it on my weblog :) ……


  123. I am happy that I observed this site , exactly the right information that I was looking for! .


  124. Many thanks for a fantastic and topical posting We certainly recognize all of the effort that went in to the creating.

  125. Homepage says:

    … [Trackback]…

    [...] Read More Infos here: codershelpdesk.com/2010/11/18/preload-images-using-jquery/ [...]…

Leave a Reply

*