Show image after loading via ajax

Asked

Viewed 734 times

0

Hello,

I’m setting up a site with no refresh. When the user clicks on the tab clients are loaded slides of the same, however as the embedded images have a 2mb and end up loading by part.

I wonder if there is an event that checks if an image added by ajax has been fully loaded. When loading the site I do with the event onload no body, but after loading the page, this event no longer works.

PS: I’ve used ajax’s Success/complete, but it doesn’t work because my ajax returns a string in html, which I then add in a div.

Thank you for your attention.

  • I believe the best is to decrease the size of the images

  • Unfortunately this possibility does not exist, and even if it existed if the user is with a slow connection the images will take to load independent of the size...

  • Managed to solve?

3 answers

2

You can make a Event load for each image that returns from Ajax and calls a function when all are loaded.

A suggestion would be to create a div hidden to receive the return of Ajax with the images, count how many images returned and make a .each() creating a on load for each one. The images will be hidden in the div, and when all are loaded, you send the HTML of that div for the the div end and makes the fadeIn().

Create a div hidden anywhere:

<div id="oculta"></div>

And put in her CSS:

#oculta{
   display: none;
}

Create a function that will make the transition:

function mostraDiv(){
   // envia o HTML da div oculta e faz o fadeIn na div principal
   $("#div-da-aba").html( $("#oculta").html() ).hide().fadeIn();
   $("#oculta").empty(); // esvazia div oculta
}

In the success ajax:

success: function(data){
   $("#oculta").html(data); // envia o retorno para a div oculta

   var imgs = $("img", "#oculta"); // busca imagens pela tag img
   var imgs_conta = imgs.length; // conta o número de imagens
   var conta = 0; // seta um contador

   // aqui eu crio um event load para cada imagem
   imgs.each(function(i,e){
      e.onload = function(){
         conta++; // quando a imagem é carregada, incremento o contador

         // se o contador alcançar o número de imagens, chamo a função
         if(conta == imgs_conta) mostraDiv();
      }
   });
}

Why did I suggest using a hidden div?

So that in the div main you can place a message of "Opening..." or "Loading..." or some animated upload while the images are being loaded in hidden.

  • I did a test with this code, but the same thing happens with the other codes, the image is loading (in the network I see this information) and it triggers the event before it finishes loading, Due to the doubts I gave a console.log in the information and it returns to me exactly the 4 images you have on the page. That is, there is no write error or logic error, only the on("load") event is returning true even without being loaded!

  • Here you can test: http://dvdteste.hosting desites.ws/stack/t5.php I put 2 images with 19mb each.

  • But okay, I’ll check it out and see.

  • The load ends with 6s and Finish ends with 11s, that may be why not? http://prntscr.com/jddv8y

  • In case the image ends when FINISH stops and not when LOAD stops...

  • Here it worked perfect, I even put a console.log to warn when each image is loaded. I only added a 1 second setTimeout in the pq function the browser takes a while to display the image even though it has been loaded.

  • I noticed on your server. Anyway, thanks for the help, I will try to solve anything update here!

  • Okay buddy. I really don’t know any other way to do this.

Show 3 more comments

0

Simply with pure Javascript:

document.getElementById('IDdaImagem').addEventListener('load',function(){ 
    this.style.display = 'block'
})
<img width="100px" heigth="100px" id="IDdaImagem" src="https://upload.wikimedia.org/wikipedia/commons/d/dd/Big_%26_Small_Pumkins.JPG" alt="" style="display: none;">

Then just do the implementation in Jquery and use the effects you want

Note: If it takes too long wait a little longer, is that I took a very large image for the effect to be very visible

0


Solution:

Well, actually from the beginning I was doing it right, the problem is that the image was loaded in a hidden div, so when I showed the hidden div with the image inside, then yes the image would be rendered.

With this, I loaded the page with all div’s visible and with the loading div over all of them, when the page finishes being loaded I hide all Divs (except the one I want to show the user) and the image appears fully rendered!

PS: It is not recommended to use images as large as 4mb. Because even after it is loaded, the rendering of it by the browser is slow and ends up being shown in parts.

Problem solved!

Thank you to everyone who followed and somehow tried to help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.