Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="picture picture__first"></div>
  <div class="picture picture__second"></div>
  <div class="picture picture__third lazy"></div>
  <div class="picture picture__fourth lazy"></div>
  <div class="picture picture__fifth lazy"></div>
</div>

              
            
!

CSS

              
                .container {
  width: 400px;
  margin: auto;
}
.picture {
  width: 400px;
  height: 300px;
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center;
  margin: 25px;
  transition: all 0.25s cubic-bezier(0.95, 0.05, 0.795, 0.035);
  &__first {
    background-image: url('https://bedekodzic.pl/wp-content/uploads/2019/01/lazy-load_beach-min.png');
  }
  &__second {
    background-image: url('https://bedekodzic.pl/wp-content/uploads/2019/01/lazy-load_branch.png');
  }
  &__third {
    margin-top: 500px;
    background-image: url('https://bedekodzic.pl/wp-content/uploads/2019/01/lazy-load_patterns.png');
  }
  &__fourth {
    margin-top: 500px;
    background-image: url('https://bedekodzic.pl/wp-content/uploads/2019/01/lazy-load_surf.png');
  }
  &__fifth {
    margin: 500px 0;
    background-image: url('https://bedekodzic.pl/wp-content/uploads/2019/01/lazy-load_travel.png');
  }
}

.lazy {
  background-image: url('');
  position: relative;
  background-color: #cbe4f9;
  &:after {
    content: '';
    position: absolute;
    background-image: url('https://svgshare.com/i/ANT.svg');
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
    width: 150px;
    height: 150px;
    margin: auto;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
}
              
            
!

JS

              
                const lazyLoad = new class LazyLoad {
  constructor() {
    this.lazyImages = document.querySelectorAll('.lazy');
    this.container = document.querySelector('.container');
    this.lazyloadThrottleTimeout;
  }
  // Lazy load for Chrome & FF
  intersector() {
    const imageObserver = new IntersectionObserver((entries) => {
      entries.forEach((entry) => {
        if (entry.isIntersecting) {
          let image = entry.target;
          image.src = image.dataset.src;
          image.classList.remove('lazy');
          imageObserver.unobserve(image);
        }
      });
    }, {
      rootMargin: '0px 0px -250px 0px'
    });

    this.lazyImages.forEach((image) => {
      imageObserver.observe(image);
    });
  }
  // Lazy load for Safari
  safariLazyLoad() {
    const lazyImages = document.querySelectorAll('.lazy');

    if (this.lazyloadThrottleTimeout) {
      clearTimeout(this.lazyloadThrottleTimeout);
    }
    this.lazyloadThrottleTimeout = setTimeout(() => {
      let top = window.scrollY;
      lazyImages.forEach((image) => {
        if (top > image.offsetTop - 250) {
          image.classList.remove('lazy');
        }
      });
    }, 20);
  }
}
'IntersectionObserver' in window ? lazyLoad.intersector() :
  document.onscroll = lazyLoad.safariLazyLoad,
  window.addEventListener('resize', lazyLoad.safariLazyLoad),
  window.addEventListener('orientationChange', lazyLoad.safariLazyLoad);
              
            
!
999px

Console