Coder Wrap provides free to use snippets Coder Wrap provides free to use snippets

Back to Images Effects
Preview Source Code
Download
HTML
CSS
/**Typeo CSS Start**/
  @import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,500;0,600;0,700;1,400&display=swap');
  * {
  box-sizing: border-box;
  }
  body {
  font-family: "Poppins", sans-serif;
  color: #444;
  box-sizing: border-box;
  margin: 0px;
  }
  /**Typeo CSS End**/
  h2 {
  text-align: center;
  font-weight: 300;
  color: #000;
  font-size: 30px;
  margin: 40px 0 0;
  }
  .gallery {
  display: flex;
  flex-wrap: wrap;
  max-width: 960px;
  margin: 40px auto;
  box-shadow: 0 0 20px #727272;
  }
  .gallery-item {
  width: 50%;
  }
  .gallery-item > img {
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  }
  .gallery-item img {
  opacity: 1;
  transition: opacity 1s;
  }
  .gallery-item img[data-src] {
  opacity: 0;
  }
JS
(function() {
 var elements = document.querySelectorAll('img[data-src]');
 var index = 0;
 var lazyLoad = function() {
 if(index >= elements.length) return;
 var item = elements[index];
 if((this.scrollY + this.innerHeight) > item.offsetTop) {
 var src = item.getAttribute("data-src");
 item.src = src;
 item.addEventListener('load', function() {
 item.removeAttribute('data-src');
 });
 index++;
 lazyLoad();
 }
 };
 var init = function() {
 window.addEventListener('scroll', lazyLoad);
 lazyLoad();
 };
 return init();
 })();

Related Snippets

Leave a comment

Your email address will not be published. Required fields are marked *