i have a page with something like:
link
link
link
link
somethingelse(div)
with my script i change the link in a img and the after i have to edit the somethingelse....
but now i have a problem.. i need the cords of the div but when i try to get it i get the cords BEFORE the img is loaded.... and i get the wrong cords....
how i can run the function after the complete load of the images?
pseudocode:
$("a").each(function(){ replaceAtoIMG(this)}); //change <a> with <img> and add a new image loading
$("div").each(function(){ $(this).position() }); //return value BEFORE the fully completed load
-
Easy. wait until the images are loaded to get the position. If you are unsure of how to do that, maybe that should have instead been your question, however that's a duplicate many times over.user400654– user4006542014年06月16日 18:08:48 +00:00Commented Jun 16, 2014 at 18:08
-
i tried with .load or ready... but i don't get it works...Trigun– Trigun2014年06月16日 18:22:39 +00:00Commented Jun 16, 2014 at 18:22
-
Nether of those methods will help you. you will have to loop over each image and detect when each one finishes. When they are all finished, you then get the position.user400654– user4006542014年06月16日 18:26:36 +00:00Commented Jun 16, 2014 at 18:26
-
oh something like $('img').on('load', function() { if ($('img').length == X) else X++ });Trigun– Trigun2014年06月16日 18:52:02 +00:00Commented Jun 16, 2014 at 18:52
-
exactly like that (ignoring syntax)user400654– user4006542014年06月16日 18:53:05 +00:00Commented Jun 16, 2014 at 18:53
1 Answer 1
$('img').on('load', function() {
// do whatever you want
});
This should solve your on load problem at least.
Sorry, was at work and using phone for stackoverflow so had to be quick.
However, if needing your function to apply to each image on load then you can wrap your on load function inside an each() function. Untested so might need a little fandangling:
$('img').each(function(){
$(this).on('load', function() {
// do whatever you want });
});
That should attach that function to all img tags and trigger when theyre done loading. Then you can use their coords or what not youre trying to achieve. Your pseudocode confused me.