views:

254

answers:

1

i have 2 img elements. 2 id's for them: rimg and limg. limg is the loading gif and rimg is the reloaded full vertsion of the image.

the is the code to show the loading before image finish to load:

$("#limg").fadeIn("fast");
$("#rimg").attr("src",$(that).attr("bsrc")).load(function(){
    $("#limg").fadeOut("fast",function () {
        $("#rimg").attr("alt",$(that).attr("alt"));
        $("#rimg").fadeIn("normal");
});
});

if i log into the site for the first time, it shows me the loading gif. if it is not my first time it is still showing that but i need the loading to be smooth without that loading how can i check if the image is loaded and cached before showing the loading gif?

A: 

I think this is the only solution for you: if you build the following into your jquery script then will work.

var pic=new Image(x,y);//x,y is optional
pic.src= //path;

if(pic.complete) donePic()
else pic.onload= donePic;

If you put it inside of a timer then you'll know when it gets loaded.

Ervin
on both cases of the if you call donePicwon't it bring you the same result?is the donePic function what i have in my code of the first question?
Y.G.J
donePic() tells you that the pic is loaded. if it enters here: if(pic.complete) donePic(), it means that the pic is cached and it is loadaed instantly, else will be loaded. In this else case the donePic() function will be called only when the pic has been loaded.But the first tag (the if part) can help you to determine if the pic is or not in the cache. After this is up to you.
Ervin
but what it does is to to call donePic right away if it is loaded or call it after pic finish loading... that means the donePic deals with the picture... not with the loading gif. there so, i will always have the loading gif shows until finishing the check.don't i need to do just on load and on complete to do nothing?
Y.G.J
if your problem is that the loading gif appears even if the image is loaded from cache, then all you have to do is if(pic.complete) donePic(); where donePic(); hides the loading gif.
Ervin
nice but a little problem with that... if i look at the page for the first time it will show me the reload and after the loop restart it will not show the reload.if i will refresh the page - it is like i was never in the page: loading gif is there altough images are cached!is there a solution?
Y.G.J
sorry, I don't know any other solutions fot this, but I'm sure you can get it working with this one.
Ervin
it's working but not when refreshing the window or reopening the browser
Y.G.J
isn't your cache disabled? Please have a look at your cache settings, maybe your cahce is full or disabled.
Ervin