Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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="l-container">
  <div class="l-container__inner">
    <div class="o-gif o-aspect-ratio">
      <img class="o-gif__lazy" src="" data-src="https://assets.codepen.io/3187356/pills.gif" data-loaded="false" alt="Animated illustration of medical pills falling">
      <button class="o-gif__button" type="button" data-canvas-show="true">
        <div class="u-v-h">Play/pause gif</div>
        <div class="o-aspect-ratio">
          <svg class="o-gif__pause" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M8 19c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2s-2 .9-2 2v10c0 1.1.9 2 2 2zm6-12v10c0 1.1.9 2 2 2s2-.9 2-2V7c0-1.1-.9-2-2-2s-2 .9-2 2z"/>
          </svg>
          <svg class="o-gif__play" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M8 6.82v10.36c0 .79.87 1.27 1.54.84l8.14-5.18c.62-.39.62-1.29 0-1.69L9.54 5.98C8.87 5.55 8 6.03 8 6.82z"/>
          </svg>
        </div>
      </button>
    </div>
  </div>
</div>

<div class="l-container">
  <div class="l-container__inner">
    <div class="o-gif o-aspect-ratio">
      <img class="o-gif__lazy" src="" data-src="https://assets.codepen.io/3187356/egg.gif" data-loaded="false" alt="Animated illustration of a fried egg flying">
      <button class="o-gif__button" type="button" data-canvas-show="true">
        <div class="u-v-h">Play/pause gif</div>
        <div class="o-aspect-ratio">
          <svg class="o-gif__pause" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M8 19c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2s-2 .9-2 2v10c0 1.1.9 2 2 2zm6-12v10c0 1.1.9 2 2 2s2-.9 2-2V7c0-1.1-.9-2-2-2s-2 .9-2 2z"/>
          </svg>
          <svg class="o-gif__play" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M8 6.82v10.36c0 .79.87 1.27 1.54.84l8.14-5.18c.62-.39.62-1.29 0-1.69L9.54 5.98C8.87 5.55 8 6.03 8 6.82z"/>
          </svg>
        </div>
      </button>
    </div>
  </div>
</div>

<div class="l-container">
  <div class="l-container__inner">
    <div class="o-gif o-aspect-ratio">
      <img class="o-gif__lazy" src="" data-src="https://assets.codepen.io/3187356/toast.gif" data-loaded="false" alt="Animated illustration of toast being tossed in and out of toaster">
      <button class="o-gif__button" type="button" data-canvas-show="true">
        <div class="u-v-h">Play/pause gif</div>
        <div class="o-aspect-ratio">
          <svg class="o-gif__pause" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M8 19c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2s-2 .9-2 2v10c0 1.1.9 2 2 2zm6-12v10c0 1.1.9 2 2 2s2-.9 2-2V7c0-1.1-.9-2-2-2s-2 .9-2 2z"/>
          </svg>
          <svg class="o-gif__play" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M8 6.82v10.36c0 .79.87 1.27 1.54.84l8.14-5.18c.62-.39.62-1.29 0-1.69L9.54 5.98C8.87 5.55 8 6.03 8 6.82z"/>
          </svg>
        </div>
      </button>
    </div>
  </div>
</div>
              
            
!

CSS

              
                
/* Base */

*,
*:before,
*:after {
  box-sizing: border-box;
}

body {
  background: black;
  margin: 0;
  padding: 0;
}

img {
  max-width: 100%;
  height: auto;
}

/* Utilities */

.u-v-h {
  position: absolute;
  overflow: hidden;
  left: -625rem;
  top: auto;
  width: 0.063rem;
  height: 0.063rem;
}

/* Layout */

.l-container {
  max-width: 30rem;
  margin: auto;
  min-height: 100vh;
  display: flex;
}

.l-container__inner {
  width: 100%;
  margin: auto;
  padding: 5%;
}

/* Objects */

.o-aspect-ratio {
  position: relative;
  overflow: hidden;
  padding-top: 100%;
  -webkit-mask-image: -webkit-radial-gradient( white, black );
}

.o-gif {
  border-radius: 1rem;
  background: #ccc;
  
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  img, 
  canvas {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
  }
}

.o-gif__lazy,
.o-gif__canvas {
  transition: opacity 250ms linear;
  opacity: 0;
  
  &[data-loaded="true"] {
    opacity: 1;
  }
}

.o-gif__button {
  position: absolute;
  bottom: 0;
  right: 0;
  margin: 0.625rem;
  z-index: 1;
  border: 0;
  border-radius: 0.563rem;
  background: rgba( 0, 0, 0, 0.5 );
  padding: 0;
  width: 10%;
  min-width: 2.188rem;
  max-width: 3.125rem;

  &[data-canvas-show="true"] {
    .o-gif__pause {
      visibility: hidden;
    }
  }

  &[data-canvas-show="false"] {
    ~ canvas,
    .o-gif__play {
      visibility: hidden;
    }
  }
}

.o-gif__pause, 
.o-gif__play {
  position: absolute;
  top: 50%;
  transform: translateY( -50% );
  left: 0;
  right: 0;
  fill: white;
  width: 85%;
  margin: auto;
}

/* Remove animations */

@media ( prefers-reduced-motion: reduce ) {

  html {
    scroll-behavior: auto;
  }

  *,
  *::before, 
  *::after {
    transition: none !important;
    transition-timing-function: linear !important;
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important; /* source ( https://bit.ly/34Nvli0 ) */
    transition-duration: 0.001ms !important;
  }

}


              
            
!

JS

              
                
/*
 * Utilities
 * ---------
 */

const imageLoaded = ( image ) => {
  return new Promise( ( resolve, reject ) => {
    let proxyImage = new Image();

    proxyImage.src = image.src;

    const res = () => {
      resolve( image );
    };

    const err = ( message ) => {
      reject( message );
    };

    if( proxyImage.complete )
      res();

    proxyImage.onload = res;
    proxyImage.onerror = err;
  } );
};

const draw = ( g, c, ctx ) => {
  const w = g.clientWidth;
  const h = g.clientHeight;

  c.width = w;
  c.height = h;

  ctx.drawImage( g, 0, 0, w, h );
};

/*
 * DOM loaded
 * ----------
 */

document.addEventListener( 'DOMContentLoaded', () => {

  /* Variables */

  const gifs = Array.from( document.querySelectorAll( '.o-gif__lazy' ) );

  let items = [];
  let resizeTimer;
  let reduceMotion = false;
  let ioSupported = false;

  /* Check if reduce motion */

  const mediaQuery = window.matchMedia( '(prefers-reduced-motion: reduce)' );

  if( !mediaQuery || mediaQuery.matches )
    reduceMotion = true;
  
  /* Check if IntersectionObserver is supported */

  if('IntersectionObserver' in window &&
     'IntersectionObserverEntry' in window &&
     'intersectionRatio' in window.IntersectionObserverEntry.prototype ) {
    ioSupported = true;
  }

  /* Event callbacks */

  const resize = () => {
    // throttles resize event
    clearTimeout( resizeTimer );

    resizeTimer = setTimeout( () => {
      if( items.length ) {
        items.forEach( item => {
          draw( item.gif, item.canvas, item.context );
        } );
      }
    }, 100 );
  };

  const click = ( e ) => {
    let button = e.currentTarget;
    let show = button.getAttribute( 'data-canvas-show' );

    button.setAttribute( 'data-canvas-show', show === "true" ? false : true );
  };
  
  /* Source: https://web.dev/lazy-loading-images/ */
  
  const lazyLoad = ( index ) => {
    const item = items[index];
    
    const observer = new IntersectionObserver( ( entries, obs ) => {
      entries.forEach( entry => {
        if( entry.isIntersecting ) {
          let lazyImage = entry.target;
          lazyImage.src = lazyImage.dataset.src;
          
          imageSetup( index );
          
          observer.unobserve( lazyImage );
        }
      } );
    } );
    
    observer.observe( item.gif );
  };
  
  const imageSetup = ( index ) => {
    const item = items[index];
    
    imageLoaded( item.gif ).then( data => {
      draw( item.gif, item.canvas, item.context );
      item.gif.parentElement.appendChild( item.canvas );
      
      if( !reduceMotion )
        item.button.click();
      
      item.canvas.setAttribute( 'data-loaded', true );
      item.gif.setAttribute( 'data-loaded', true );
    } ).catch( ( err ) => {
      console.log( err );
      item.button.style.display = 'none';
    } );
  };

  /* Loop through gifs */

  if( gifs.length ) {
    
    gifs.forEach( ( g, index ) => {
      const c = document.createElement( 'canvas' );
      const ctx = c.getContext( '2d' );
      const button = g.nextElementSibling;
      
      c.setAttribute( 'class', 'o-gif__canvas' );
      c.setAttribute( 'data-loaded', 'false' );

      items.push( {
        gif: g, 
        canvas: c, 
        context: ctx,
        button: button,
      } );
      
      if( ioSupported ) {
        lazyLoad( index );
      } else {
        g.src = g.getAttribute( 'data-src' );
        imageSetup( index );
      }

      button.addEventListener( 'click', click );
    } );

    window.addEventListener( 'resize', resize );
  }

} );

              
            
!
999px

Console