Search code examples
jqueryimageloadfadein

FadeIn image after it is loaded


I'm trying to make image being loaded into div with fadeIn effect. Problem is that I don't know how to avoid loading and fading at the same time. I want image to be loaded and after it is completely loaded it should be faded in.

http://www.izrada-weba.com/vedranmarketic

These are image thumbs:

<div id="thumbs">
                <a href="#" class="slika_thumb" id="1"><img src="slike/th.jpg" border="0"/></a><a href="#" class="slika_thumb" id="2"><img src="slike/th.jpg" border="0"/></a><a href="#" class="slika_thumb" id="3"><img src="slike/th.jpg" border="0"/></a><a href="#"><img src="slike/th.jpg" border="0"/></a><a href="#"><img src="slike/th.jpg" border="0"/></a><a href="#"><img src="slike/th.jpg" border="0"/></a> </div>
        </div>

This is container where image should be loaded:

<div id="desna_kolona">
            <div id="slika"><img src="slike/c6.jpg" /></div>
        </div>

and this is jquery file:

$(document).ready(function(){

    $('.slika_thumb').click(function() {
        var id = $(this).attr("id");
        $('#slika').hide();

        $.ajax({
          url: 'slike/slika.php?id=' + id,
          success: function(data) {

            $('#slika').html(data);
            $('#slika').fadeIn();
          }
        });    

    });

});

I tried to use complete below success but still the same result. Any advice?


Solution

  • Maybe try to bind an onload handler to any images inside the data which gets loaded via ajax.

    $(document).ready(function(){
    
      $('.slika_thumb').click(function() {
          var id = $(this).attr("id");
          $('#slika').hide();
    
          $.ajax({
            url: 'slike/slika.php?id=' + id,
            success: function(data) {
    
              $('#slika').html(data);
              $('#slika img').bind("load", function() {
                $('#slika').fadeIn();  
              });
            }
          });    
    
      });
    
    });