开发者

Show div after individual image loads

Lets say I have this kind of set of images:

<div class="image"><img src="image1.jpg" alt="" />image1</div>
<div class="image"><img src="image2.jpg" alt="" />image2</div>
<div class="image"><img src="image3.jpg" alt="" />image3</div>

(note that the divs would be initially hidden)

What I want is that, after an individual <image> has loaded, .show(); the div.image surrounding it.

I would 开发者_开发知识库think this would be easy to do but I have searched all over the internet and so far they've been about doing something after all images have loaded instead of individual images.

Is this even possible to do?

Edit: It was later on changed so that there's also link around the image Working example: http://jsfiddle.net/peeter/qwtWZ/6/


<div class="image"><img src="http://acupuncture.celeboutfit.com/images/img/IMG_5400m-1.jpg" alt="" />image1</div>
<div class="image"><img src="image2.jpg" alt="" />image2</div>
<div class="image"><img src="image3.jpg" alt="" />image3</div>
<script type="text/javascript">
$(document).ready(function(){
    $("div.image").hide();
    $("div.image").find("img").load(function(){
        $(this).closest("div.image").show(500);
    });
});
</script>

Working example: http://jsfiddle.net/peeter/qwtWZ/6/


$(".image img").load(function()
{
    $(this).closest(".image").show();
});

Edit: Updated to allow for images that are descendants, but not necessarily children.


$(document).ready(function(){
  $('img').load(function(){
    $(this).parent().show();
  });
});

should work just fine !


You could load the images using jQuery like so

$("#img1").load('image1.jpg',function(){ $("#img1").show(); });
$("#img2").load('image2.jpg',function(){ $("#img2").show(); });
$("#img3").load('image3.jpg',function(){ $("#img3").show(); });

Where you give the above IDs to your divs. The function in the load() function is called when the resource has finished loading.

See http://api.jquery.com/load/ for the function reference.


I had to do this to prevent a FOUC but I didn't want to load awesome but heavy jQuery right at the top of my page so I wrote this utility script. In my example the parent container of the image is hidden until the image has been loaded. Works for me.

(function() {

  function getArrayFromNodeList(list) {
    return Array.prototype.slice.call(list);
  }

  function ready() {
    var imageContent = document.querySelectorAll('.hero, .key-features__feature'),
      contentArray = getArrayFromNodeList(imageContent);

    contentArray.forEach(function(content) {
      var images = getArrayFromNodeList(content.querySelectorAll('img'));

      images.forEach(function(img) {
        img.onload = function() {
          console.log('img loaded');
          content.style.display = 'block';
        };
      });
    });
  }

  document.addEventListener("DOMContentLoaded", ready);

}());
.hero,
.keyfeatures__feature {
  display: none;
}
<!-- Example of one piece of content -->
<article class="hero">
  <!-- This is the image I want to make sure has loaded -->
  <img src="https://placeimg.com/978/400/animals/grayscale" alt="">

  <!-- other HTML content -->
</article>
<!-- /hero -->

<!-- Example of another piece of content -->
<article class="key-features">
  <div class="key-features__feature">
    <img src="https://placeimg.com/480/320/animals/grayscale" alt="">
  </div>
  <!-- /key-features__feature -->

  <div class="key-features__feature">
    <img src="https://placeimg.com/480/320/animals/grayscale" alt="">
  </div>
  <!-- /key-features__feature -->
</article>
<!-- /key-features 6-6 -->

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜