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

              
                <h1>imagesLoaded v5, progress with jQuery</h1>

<div class="buttons">
  <button id="add">Add images</button>
  <button id="reset">Reset</button>
</div>
<div id="status">
  <progress max="7" value="0"></progress>
</div>
<div id="image-container"></div>

              
            
!

CSS

              
                body {
  font-family: sans-serif;
}

#image-container img {
  max-height: 140px;
}

li {
  height: 140px;
  min-width: 100px;
  display: block;
  float: left;
  list-style: none;
  margin: 0 5px 5px 0;
  background-color: black;
  background-position: center center;
  background-repeat: no-repeat;
}

li img,
#status {
  -webkit-transition: opacity 0.4s;
     -moz-transition: opacity 0.4s;
      -ms-transition: opacity 0.4s;
          transition: opacity 0.4s;
}

li.is-loading {
  background-color: black;
  background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/loading.gif');
}

li.is-broken {
  background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/broken.png');
  background-color: #be3730;
  width: 120px;
}

li.is-loading img,
li.is-broken img {
  opacity: 0;
}

.buttons { margin-bottom: 1.0em; }

button {
  font-size: 18px;
  padding: 0.4em 0.8em;
  font-family: sans-serif;
}

#status {
  opacity: 0;
  position: fixed;
  right: 20px;
  top: 20px;
  background: hsla( 0, 0%, 0%, 0.8);
  padding: 20px;
  border-radius: 10px;
  z-index: 2; /* over other stuff */
}
              
            
!

JS

              
                let $container = $('#image-container');
let $status = $('#status');
let $progress = $('progress');

let supportsProgress = $progress[0] &&
  // IE does not support progress
  $progress[0].toString().indexOf('Unknown') === -1;

let loadedImageCount, imageCount;

$('#add').click( function() {
  // add new images
  let items = getItems();
  $container.prepend( $(items) );
  // use ImagesLoaded
  $container.imagesLoaded()
    .progress( onProgress )
    .always( onAlways );
  // reset progress counter
  imageCount = $container.find('img').length;
  resetProgress();
  updateProgress( 0 );
});

// reset container
$('#reset').click( function() {
  $container.empty();
});

// -----  ----- //

// return doc fragment with
function getItems() {
  let items = '';
  for ( let i = 0; i < 7; i++ ) {
    items += getImageItem();
  }
  return items;
}

// return an <li> with a <img> in it
function getImageItem() {
  let item = '<li class="is-loading">';
  let size = Math.random() * 3 + 1;
  let width = Math.random() * 110 + 100;
  width = Math.round( width * size );
  let height = Math.round( 140 * size );
  let rando = Math.ceil( Math.random() * 1000 );
  // 10% chance of broken image src
  // random parameter to prevent cached images
  let src = rando < 100 ? `//foo/broken-${rando}.jpg` :
    // use picsum.photos for great random images
    `https://picsum.photos/${width}/${height}/?${rando}`;
  item += `<img src="${src}" /></li>`;
  return item;
}

// -----  ----- //

function resetProgress() {
  $status.css({ opacity: 1 });
  loadedImageCount = 0;
  if ( supportsProgress ) {
    $progress.attr( 'max', imageCount );
  }
}

function updateProgress( value ) {
  if ( supportsProgress ) {
    $progress.attr( 'value', value );
  } else {
    // if you don't support progress elem
    $status.text( value + ' / ' + imageCount );
  }
}

// triggered after each item is loaded
function onProgress( imgLoad, image ) {
  // change class if the image is loaded or broken
  let $item = $( image.img ).parent();
  $item.removeClass('is-loading');
  if ( !image.isLoaded ) {
    $item.addClass('is-broken');
  }
  // update progress element
  loadedImageCount++;
  updateProgress( loadedImageCount );
}

// hide status when done
function onAlways() {
  $status.css({ opacity: 0 });
}

              
            
!
999px

Console