css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <section>
    <h1>Retaillez/Scrollez votre fenêtre</h1>
  <h2>5 fois la même image, une url, un chargement (voir en bas)</h2>
  <div>
    No Lazy Load
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" alt="" />
    </div>
  </div>
  <div>
    Simple Lazy Load
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" src="" />
    </div>
  </div>
  <div>
    Lazy Load min-width 1000px
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-min-width="1000" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
  <div>
    CLICK ME ! LL only after Click
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-src-click="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg"
        alt="" />
    </div>
  </div>
</section>
<section>
  <div>
    No Lazy Load
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" alt="" />
    </div>
  </div>
  <div>
    Lazy Load on Scroll
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-visible="0" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" src="" />
    </div>
  </div>
  <div>
    Lazy Load on Scroll and Width
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-visible="0" data-min-width="1000" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
  <div>
    Lazy Load on Scroll
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-visible="100" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg"
        alt="" />
    </div>
  </div>
</section>
            
          
!
            
              /** Le CSS ici n'est pas interessant pour la compréhension de cet exemple. **/

/** Ce qui importe sont les « data-src », « data-min-width », « data-src-click » ou « data-visible » dans les tag `<img>` et le JavaScript de la partie de droite **/

/** Le but ici est 
- d'afficher les images de plus basse résolution dans un premier temps
- puis quand la page est chargée, d'afficher la meilleur qualité possible de chaque image « visible ».
- puis de charger celle des autres images uniquement quand elle apparaissent à l'écran. **/

/** j'ai laissé une image basse résolution et une image a chargé en haute qualité uniquement avec le clique. **/





/** -- N'est pas interessant pour la partie Lazy Load -- **/
.canvas {
	position: relative;
	width: 100%;
  overflow: hidden;
}
.canvas:before {
	content: "";
	display: block;
	padding-top: 100%;
}
.content {
	position: absolute;
	top: 0;
	height: 100%;
}

section {
  margin: 0 auto;
  max-width: 1070px;
  padding-bottom: 100px;
}
section:after {
content: "";
  display: block;
  clear: left;
}
div:not(.canvas) {
  width: calc(25% - 8px);
  float: left;
  padding: 2px;
  border: 1px solid #000;
  margin: 4px;
  box-sizing: border-box;
}
img[data-src-click] {
  cursor: pointer;
}
            
          
!
            
              function lazyLoad(targets, attrSource, callback) {
  var images = targets || document.querySelectorAll("img[data-src]"),
    source = attrSource || "data-src";

  function checkVisible(element) {
    var rect = element.getBoundingClientRect(),
      viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
    return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
  }

  [].forEach.call(images, function(image) {
    var minWidth = image.getAttribute("data-min-width"),
        visible = image.getAttribute("data-visible");
    
    if (
      !minWidth || 
      (minWidth && window.matchMedia("(min-width: " + minWidth + "px)").matches)
    ) {
      if (
        !visible ||
        (visible && checkVisible(image))
      ) {
        image.addEventListener("load", function(event) {
          if (callback) {
            callback(event);
          }
        });
        image.src = image.getAttribute(source);
      }
    }
  });
}

// Simple Lazy Load
window.addEventListener("DOMContentLoaded", function() {
  lazyLoad();
});

// Lazy Load min-width 1000px
window.addEventListener("resize", function() {
  lazyLoad();
});

// CLICK ME ! LL only after Click
document.querySelector("img[data-src-click]")
  .addEventListener("click", function(e) {
  lazyLoad([e.target], "data-src-click", function(e) {
    alert(e.target.getAttribute("data-src-click") + " is loaded !");
  });
});

// Lazy Load on Scroll
window.addEventListener("scroll", function() {
  lazyLoad();
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console