title: Combine Js Lazy load (no external plugin) with responsive images breakpoints data-srcset . How should I fix this?
body: <p><strong>I'm trying to combine Js Lazy load with responsive images breakpoints.</strong></p>
  <p>The algorithmic concept is to find image width values that offer a significant reduction in file size. Images are analyzed to find the best breakpoints on an image by image basis, rather than creating all possible image resolutions, and enables developers to easily create 'picture' and 'img' HTML5 elements based on the calculated breakpoints.</p>
  <p>Everything works fine with the lazy-loading, but the  <code>data-srcset</code> is not working as it should, unfortunately. The image displayed is always the one from the <code>data src</code> so the image breakpoints is not working at all.</p>
  <p>I've made a Codepen to illustrate the problem.
  HERE: <strong><a href="https://codepen.io/cat999/pen/bGEKwNE" rel="nofollow noreferrer">https://codepen.io/cat999/pen/bGEKwNE</a></strong></p>
  <p>How should I fix this?</p>
  <pre><code>$(document).ready(function() {
    var lazyloadImages;    
  
    if (&quot;IntersectionObserver&quot; in window) {
      lazyloadImages = document.querySelectorAll(&quot;.lazy&quot;);
      var imageObserver = new IntersectionObserver(function(entries, observer) {
        entries.forEach(function(entry) {
          if (entry.isIntersecting) {
            var image = entry.target;
            image.src = image.dataset.src;
            image.classList.remove(&quot;lazy&quot;);
            image.classList.add(&quot;animated&quot;);
            image.classList.add(&quot;fadeIn&quot;);
            imageObserver.unobserve(image);
          }
        });
      });
  
      lazyloadImages.forEach(function(image) {
        imageObserver.observe(image);
      });
    } else {  
      var lazyloadThrottleTimeout;
      lazyloadImages = $(&quot;.lazy&quot;);
      
      function lazyload () {
        if(lazyloadThrottleTimeout) {
          clearTimeout(lazyloadThrottleTimeout);
        }    
  
        lazyloadThrottleTimeout = setTimeout(function() {
            var scrollTop = $(window).scrollTop();
            lazyloadImages.each(function() {
                var el = $(this);
                if(el.offset().top - scrollTop &lt; window.innerHeight) {
                  var url = el.attr(&quot;data-src&quot;);
                  el.attr(&quot;src&quot;, url);
                  el.removeClass(&quot;lazy&quot;);
                  lazyloadImages = $(&quot;.lazy&quot;);
                }
            });
            if(lazyloadImages.length == 0) { 
              $(document).off(&quot;scroll&quot;);
              $(window).off(&quot;resize&quot;);
            }
        }, 20);
      }
  
      $(document).on(&quot;scroll&quot;, lazyload);
      $(window).on(&quot;resize&quot;, lazyload);
    }
  })
  </code></pre>
  <pre><code>
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, user-scalable=no&quot;&gt;
  
  
  
  &lt;img src=&quot;https://ik.imagekit.io/demo/img/image1.jpeg?tr=w-400,h-300&quot; /&gt;
  &lt;img src=&quot;https://ik.imagekit.io/demo/img/image2.jpeg?tr=w-400,h-300&quot; /&gt;
  &lt;img src=&quot;https://ik.imagekit.io/demo/img/image3.jpg?tr=w-400,h-300&quot; /&gt;
  &lt;img
    alt=&quot;Image 03&quot; class=&quot;lazy&quot; src=&quot;https://via.placeholder.com/220x280?text=Img+03&quot;
    data-src=&quot;https://www.matteogiordano.info/img/castle_c_scale,w_200.jpg&quot;
    data-srcset=&quot;https://www.matteogiordano.info/img/castle_c_scale,w_200.jpg 200w,
  https://www.matteogiordano.info/img/castle_c_scale,w_364.jpg 364w,
  https://www.matteogiordano.info/img/castle_c_scale,w_486.jpg 486w,
  https://www.matteogiordano.info/img/castle_c_scale,w_591.jpg 591w,
  https://www.matteogiordano.info/img/castle_c_scale,w_681.jpg 681w,
  https://www.matteogiordano.info/img/castle_c_scale,w_764.jpg 764w,
  https://www.matteogiordano.info/img/castle_c_scale,w_846.jpg 846w,
  https://www.matteogiordano.info/img/castle_c_scale,w_922.jpg 922w,
  https://www.matteogiordano.info/img/castle_c_scale,w_993.jpg 993w,
  https://www.matteogiordano.info/img/castle_c_scale,w_1064.jpg 1064w,
  https://www.matteogiordano.info/img/castle_c_scale,w_1138.jpg 1138w,
  https://www.matteogiordano.info/img/castle_c_scale,w_1201.jpg 1201w,
  https://www.matteogiordano.info/img/castle_c_scale,w_1261.jpg 1261w,
  https://www.matteogiordano.info/img/castle_c_scale,w_1329.jpg 1329w,
  https://www.matteogiordano.info/img/castle_c_scale,w_1393.jpg 1393w,
  https://www.matteogiordano.info/img/castle_c_scale,w_1397.jpg 1397w,
  https://www.matteogiordano.info/img/castle_c_scale,w_1400.jpg 1400w&quot;
    data-sizes=&quot;220px&quot;/&gt;
  </code></pre>