0

is this function that i wrote in jquery, will be a good way to preload? thank you

function loader(){ imgTag('#check'); function imgTag(whereToAppend){ var image = 'image.jpg'; $(whereToAppend).append('<img id="imgWrapper">'); $('#imgWrapper').attr('src', image).hide().one('load',function(){ $(this).fadeIn('slow'); }) } } 
0

1 Answer 1

1

I would suggest using the following solution to preload images:

function preload(arrayOfImages) { $(arrayOfImages).each(function(){ $('<img/>')[0].src = this; // Alternatively you could use: // (new Image()).src = this; }); } // Usage: preload([ 'img/imageName.jpg', 'img/anotherOne.jpg', 'img/blahblahblah.jpg' ]); 

Source: Preloading images with jQuery

Sign up to request clarification or add additional context in comments.

6 Comments

jQuery returns a collection, and you only need the first element. Hence you use [0] or .get(0)
so the img tag is hide until image is fully downloaded?
You need to show the images manually. They are just preloaded here. So you get rid of the loading-effect of the images. Just add the images that youre planning to show in the array and show them as usual whenever you like
ok! thanks for our quick reply. but i'm still confused of preloading...
as i understood the array of images will added on DOM but they are not showed on page still i call them. isn't it?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.