Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an image using data-src and not just src

I am trying to load an image using the img data-src tag instead of just the img src. I have created two simple JSFiddle's although only the one with the img src works. These are here:

img data-src example THIS DOESN'T WORK AND I WANT IT TO

img src example THIS ONE DOES WORK.

Can somebody please fill in the blanks as to why the img data-src one doesn't work please? I am confused by this and been searching for an answer for hours.

like image 815
Henry Brown Avatar asked Jan 08 '17 13:01

Henry Brown


People also ask

What is the difference between data src and src?

src will render the value immediately and it's the default for images, videos using a single source and iframes. data-src is used when lazy loading to prevent the default image from loading when the page loads.

Why is my image src not working?

Img src Not Working That means, when a web page loads, the browser has to retrieve the image from a web server and display it on the page. The broken link icon means that the browser could not find the image. If you've just added the image, then check that you included the correct image URL in the source attribute.

Does an IMG tag need a src?

The <img> tag has two required attributes: src - Specifies the path to the image. alt - Specifies an alternate text for the image, if the image for some reason cannot be displayed.

How do I link an image to a src?

To use image as a link in HTML, use the <img> tag as well as the <a> tag with the href attribute. The <img> tag is for using an image in a web page and the <a> tag is for adding a link. Under the image tag src attribute, add the URL of the image. With that, also add the height and width.


1 Answers

You are using HTML5 data attributes which don't replace the normal HTML attributes of an HTML element, such as src. So your image needs to have a src attribute, whether it has a data-src or not, they are both independent of each other.

data-* attributes allow us to store extra information on standard, semantic HTML elements (...)


  • Loading an image when it appears on the screen:

A common approach to lazy-loading images is to set the src to a very small image, sometimes a 1x1px gif, and once the user scrolls and the image is on the screen replace the src with the real one. Something like this:

<img src="fake_small_image.gif" data-src="real_image.jpg">

That data-src could be called data-whatever_you_want. The idea is that using JavaScript you track the scrollTop position of the page. Once the image is going to appear you replace the src value "fake_small_image.gif" with the data-src value "real_image.jpg". The example you post in the comments of this answer, is ignoring the assignment of an initial src which is invalid.

var $window = $(window),
  window_height = $window.height() - 150, // I'm using 150 (a random number) so the image appears 150px after it enters the screen, so the effect can be appreciated
  $img = $('img.some_img'),
  img_loaded = false,
  img_top = $img.offset().top;

$window.on('scroll', function() {

  if (($window.scrollTop() + window_height) > img_top && img_loaded == false) {

    $img.attr('src', $img.attr('data-src_of_original_img'));

  }

});
#container {
  width: 100%;
  height: 200vh;
  background-color: grey;
}
.some_img {
  position: absolute;
  top: 100vh;
  width: 100%;
}
body {
  margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-src_of_original_img="https://i.imgur.com/Lcsolww.jpg" alt="" class="some_img">
</div>

  • Show an image as soon as it is loaded:

A similar approach is to load the image virtually with JavaScript and once it is loaded assign the src to the image. This is done to prevent the image from showing before it is totally loaded.

var $img = $('img.some_img'),
  $img_created_with_js = $('<img src="' + $img.attr('data-src_of_original_img') + '">');

$img_created_with_js
  .on('load', function() {

    $img.attr('src', $img.attr('data-src_of_original_img'));

  });
.some_img {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-src_of_original_img="https://i.imgur.com/Lcsolww.jpg" alt="" class="some_img">

Both methods could be applied to an image. For example: you could wait until the user scrolls where the image is and then start to load it, but not show until it is fully loaded.


Resources:

  • Smallest data URI image possible for a transparent image
  • Image from Oskar Krawczyk
like image 187
Alvaro Avatar answered Oct 05 '22 23:10

Alvaro