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

              
                 <main>
    <div class="gallery">
      <figure>
      <img data-src="https://images.unsplash.com/photo-1615497001839-b0a0eac3274c?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYzMzY2ODkyNg&ixlib=rb-1.2.1&q=85&w=500" alt="">
    </figure>
      <figure>
        <img data-src="https://images.unsplash.com/photo-1572344509481-0693a88366fb?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYzMzY2ODkyNg&ixlib=rb-1.2.1&q=85&w=500" alt="">
      </figure>
      <figure>
        <img data-src="https://images.unsplash.com/photo-1604713964671-92a2c1f6f6c3?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYzMzY2ODkyNg&ixlib=rb-1.2.1&q=85&W=500" alt="">
      </figure>
      <figure>
        <img data-src="https://images.unsplash.com/photo-1586765913470-31ce41ada758?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYzMzY2ODkyNg&ixlib=rb-1.2.1&q=85&w=500" alt="">
      </figure>
      <figure>
        <img data-src="https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYzMzY2ODkyNg&ixlib=rb-1.2.1&q=85&w=500" alt="">
      </figure>
      <figure>
        <img data-src="https://images.unsplash.com/photo-1586042091284-bd35c8c1d917?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYzMzY2ODkyNg&ixlib=rb-1.2.1&q=85&w=500" alt="">
      </figure>
      <figure>
        <img data-src="https://images.unsplash.com/photo-1591608971376-46e64aa7fd19?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYzMzY2OTUzOQ&ixlib=rb-1.2.1&q=85&w=500" alt="">
      </figure>
      <figure>
        <img data-src="https://images.unsplash.com/photo-1605450648855-63f9161b7ef7?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYzMzY2OTUzOQ&ixlib=rb-1.2.1&q=85&w=500" alt="">
      </figure>
    </div>
  </main>

<script type="text/template" id="progress-content">
  <div class="progress__bar">
    <div class="lds-grid"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
  </div>
  <div class="progress__text">Loading</div>
</script>
              
            
!

CSS

              
                $size: 20vw;

@import url("https://fonts.googleapis.com/css?family=Raleway:400,400i,700");

:root {
  --dark: #000;
  --white: #fff;
  --gray: #666;
}

html, body {
  background: var(--dark);
  color: var(--white);
}

body {
  font-family: Raleway, sans-serif;
  font-size: 1rem;
  
  &:not(.loaded) {
    main {
      opacity: 0;
    }
  }
}


main {
  padding: 5vw 10vw
}

.gallery {
  display: grid;
  grid-template-columns: repeat(4, $size);
  grid-auto-rows: $size;
  
  figure {
    width: $size;
    height: $size;
    overflow: hidden;
    display: flex;
    flex-direction: center;
    align-items: center;
    
    img {
      object-fit: cover;
      width: 100%;
      height: 100%;
    }
  }
}

.progress {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  width: 100vw;
  height: 100vh;

  &__bg {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    display: flex;
    background-color: var(--dark);
  }

  &__content {
    display: flex;
    width: 100%;
    height: 100%;
    align-items: center;
    justify-content: center;
    position: relative;
    flex-direction: column;
    z-index: 1;
  }
  
  &__text {
    margin-top: 5px;
  }
}

// https://loading.io/css/
.lds-grid {
  display: inline-block;
  position: relative;
  width: 80px;
  height: 80px;
}
.lds-grid div {
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  animation: lds-grid 1.2s linear infinite;
}
.lds-grid div:nth-child(1) {
  top: 8px;
  left: 8px;
  animation-delay: 0s;
}
.lds-grid div:nth-child(2) {
  top: 8px;
  left: 32px;
  animation-delay: -0.4s;
}
.lds-grid div:nth-child(3) {
  top: 8px;
  left: 56px;
  animation-delay: -0.8s;
}
.lds-grid div:nth-child(4) {
  top: 32px;
  left: 8px;
  animation-delay: -0.4s;
}
.lds-grid div:nth-child(5) {
  top: 32px;
  left: 32px;
  animation-delay: -0.8s;
}
.lds-grid div:nth-child(6) {
  top: 32px;
  left: 56px;
  animation-delay: -1.2s;
}
.lds-grid div:nth-child(7) {
  top: 56px;
  left: 8px;
  animation-delay: -0.8s;
}
.lds-grid div:nth-child(8) {
  top: 56px;
  left: 32px;
  animation-delay: -1.2s;
}
.lds-grid div:nth-child(9) {
  top: 56px;
  left: 56px;
  animation-delay: -1.6s;
}

@keyframes lds-grid {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}
              
            
!

JS

              
                interface PreloaderOptions {
  onProgress?: Function
}

interface ImageOption {
  src: string
  img: HTMLImageElement
}

// Simple Images Preloader
class Preloader {
  public images: ImageOption[]
  public total: number
  private _count: number
  private _options: PreloaderOptions

  constructor(_options: PreloaderOptions) {
    const defaultOptions = {
      onProgress: () => {},
    }

    this.images = this.getImages()

    this.total = this.images.length
    this._count = 0

    this._options = { ...defaultOptions, ..._options }
  }

  private getImages() {
    const options = []
    const images = document.querySelectorAll('img')
    images.forEach(img => {
      const src = img.getAttribute('data-src')
      if (!src) return
      options.push({ src, img })
    })

    return options
  }

  private async _loadImages() {
    await Promise.all(
      this.images.map(option => {
        return new Promise(async resolve => {
          let img = new Image()
          img.src = option.src
          img.onload = () => {
            option.img.src = option.src
            this._count++
            this._options.onProgress(Math.round(this._count))
            resolve(option)
          }
          img.onerror = err => {
            console.error(`Failed to load image file - ${option.src}`)
            this._count++
            this._options.onProgress(Math.round(this._count))
            resolve(option)
          }
        })
      })
    )
  }

  public start() {
    return this._loadImages()
  }
}


interface ProgressOptions {
  onComplete?: gsap.Callback
}

// Loadng Progress
class Progress {
  public wrapper: HTMLElement | null
  public bg: HTMLElement | null
  public content: HTMLElement | null
  public indicator: HTMLElement | null

  public tl: GSAPTimeline | null
  private _options: ProgressOptions

  constructor(_options: ProgressOptions) {
    const defaultOptions = {
      onComplete: () => {},
    }

    this._options = { ...defaultOptions, ..._options }

    this._createElements()
  }

  private _createElements() {
    this.wrapper = document.createElement('div')
    this.wrapper.classList.add('progress')
    this.bg = document.createElement('div')
    this.bg.classList.add('progress__bg')
    this.content = document.createElement('div')
    this.content.classList.add('progress__content')
    
    const template = document.getElementById('progress-content')
    this.content.innerHTML = template.innerHTML
    
    this.wrapper.appendChild(this.bg)
    this.wrapper.appendChild(this.content)

    document.body.appendChild(this.wrapper)
  }
  
  public hide() {
    document.body.classList.add('loaded')
    this._closeAnimation()
  }
  
  private _closeAnimation() {
    this.tl = gsap.timeline({
      paused: true,
      onComplete: this._onComplete.bind(this),
    })

    // more animation ...
    
    this.tl.to(this.wrapper, {
      duration: 1,
      opacity: 0,
      ease: 'expo.out',
    })

    this.tl.play()
  }

  private _onComplete() {
    this._options.onComplete()

    this.destroy()
  }
  
  public destroy() {
    this.tl.kill()
    this.tl = null

    document.body.removeChild(this.wrapper)
    this.wrapper = null
    this.bg = null
    this.content = null
  }
}


// Gallery Animation
const gallery = gsap.from('figure', {
  opacity: 0,
  duration: 0.8,
  stagger: 0.2,
  ease: 'power1.inOut',
  paused: true
})

const progress = new Progress({
  onComplete: () => {
    console.log('progress complete')
    gallery.play()
  }
})

const preloader = new Preloader({
  onProgress: count => {
    console.log('progress:', count)
  },
})

preloader.start().then(() => {
  console.log('preload complete')
  progress.hide()
})
              
            
!
999px

Console