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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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='box'>

  <div class='animatedimage'>
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/1.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/2.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/3.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/4.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/5.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/6.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/7.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/8.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/9.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/10.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/11.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/12.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/13.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/14.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/15.png' width='128' height='128' />
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/16.png' width='128' height='128' class='poster' />
    <!-- With this preloader, the 'empty' frame is the last frame, so we set image 16 to be the poster. animation will always start from the first image, which works well in this case -->
  </div>

  <p>Image sequence<br />16 pngs = 93.9KB</p>

</div>



<div class='box'>

  <div class='animatedimage' data-spritesize='128' data-spritedirection='vertical' style='height:128px;'>
    <!-- If you want it to take the correct space before it is activated, a width/height style on .animatedimage is needed in the HTML -->
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/preloader2.png' width='128' height='2048' class='poster' style='top:-1920px;' />
    <!-- starting top/left sets the 'loading' frame -->
  </div>

  <p>Vertical sprite sheet<br />1 png = 7.7KB</p>

</div>



<div class='box'>

  <div class='animatedimage' data-spritesize='128' data-spritedirection='horizontal'>
    <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/preloader.png' width='2048' height='128' />
    <!-- For a sprite image, if you don't add class poster it won't be visible until the plugin activates it. -->
  </div>

  <p>Horizontal sprite sheet<br />1 png = 5.4KB</p>

</div>

<p>Click each box to pause/resume its animation</p>



<!-- preloader from http://preloaders.net -->
              
            
!

CSS

              
                /* styles needed by the plugin */
.animatedimage {
  position: relative;
  display: inline-block;
  line-height: 0;
  overflow: hidden;
}
.animatedimage > * {
  position: absolute;
  display: inline-block;
  visibility: hidden;
  border: 0;
}
/* the image that will show while waiting for javascript to load, and what users without javascript will see - if you don't want to use a class you might use *:first-child to select this instead */
.animatedimage > .poster {
  position: static;
  visibility: visible;
}
/* spritesheets will rely on left/top positioning */
.animatedimage[data-spritesize] > * {
  position: relative;
}




/* demo styles */
body {
  padding: 10px;
  margin: 0;
  text-align: center;
  font: normal 12px sans-serif;
  background: #111;
  color: #FFF;
}
p {
  float: left;
  margin: 10px;
}
.box {
  float: left;
  width: 128px;
  margin: 10px;
  padding: 20px;
  background: #FFF;
  border-radius: 5px;
  color: #111;
  cursor: pointer;
}
.box p {
  float: none;
  margin: 20px 0 0 0;
}
.box:hover {
  box-shadow: 0 0 0 1px #FFF;
}
.animatedimage {
  pointer-events: none; /* the image sequence has a little trouble with click events when rapidly changing the images */
}
              
            
!

JS

              
                // make sure all the images have loaded before starting any animation
$(window).on('load', function() {

  // just adding a little delay at the start to extend the 'loading' to see what is shown before the plugin is activated
  TweenLite.delayedCall(2, function() {
    // activate animation on the animatedimage elements
    $('.animatedimage').animateimage(10, -1)
    // setup a click on the box to toggle the animation pause
    .parent().on('click', function() {
      var image = $(this).children('.animatedimage');
      
      // the plugin saves the animation in the 'animation' property of the DOM element e.g. image[0].animation
      if (image.prop('animation').paused()) {
        image.prop('animation').resume();
      } else {
        image.prop('animation').pause();
      }
    });
  });
});



;(function($) {
  $.fn.animateimage = function(framerate, repeats) {
    return this.each(function() {

      var $this = $(this);
        
      framerate = Math.abs(framerate || 10);
      if (typeof repeats === 'undefined' || repeats < -1) repeats = -1;
      var duration = 1 / framerate;
      var spritesize = $this.data('spritesize');
      
      // grab all children of the target element - these should just be the image/s to animate
      var image = $this.children();
      

      if (spritesize) { // sprite sheet
        if (image.length === 1) { // image should be a single image containing the sprite sheet
          
          var horizontal = ($this.data('spritedirection') !== 'vertical');
          var spritecount = (horizontal ? image.width() : image.height()) / spritesize;
  
          TweenLite.set(image, { visibility: 'visible' });
          
          // attach a reference to the animation on the element, so it can be easily grabbed outside of the plugin and paused, reversed etc
          this.animation = new TimelineMax({ repeat: repeats });
  
          if (horizontal) {
            TweenLite.set(this, { width: spritesize });
            for (var i = 0; i < spritecount; i++) {
              this.animation.set(image, { left: "-" + (i*spritesize) + "px" }, i*duration);
            }
          } else {
            TweenLite.set(this, { height: spritesize });
            for (var i = 0; i < spritecount; i++) {
              this.animation.set(image, { top: "-" + (i*spritesize) + "px" }, i*duration);
            }
          }
          // add an 'empty' set after the last position change - this adds padding at the end of the timeline so the last frame is displayed for the correct duration before the repeat
          this.animation.set({}, {}, i*duration);
        }

      } else { // image sequence
        if (image.length > 1) { // image should only contain the images to be animated
          
          // styles for hidden and visible image in the sequcnce
          var hidden = { position: 'absolute', visibility: 'hidden' };
          var visible = { position: 'static', visibility: 'visible' };
    
          // in case the poster is not the first child, make sure its pre-animated state is disabled
          TweenLite.set(image.filter('.poster'), hidden);
    
          var lastimage = image.last();
          
          // attach a reference to the animation on the element, so it can be easily grabbed outside of the plugin and paused, reversed etc
          this.animation = new TimelineMax({ repeat: repeats })
              // this first set is not strictly needed as lastimage is underneath all of the other images, but it certainly doesn't hurt
              .set(lastimage, hidden)
              // toggle images one by one between visible and hidden - at any one time, only one image will be visible, and its static positioning will set the size for the container
              .staggerTo(image, 0, visible, duration, 0)
              // hide all the elements except lastimage - it will be hidden on repeat if needed at the same time as first is shown
              .staggerTo(image.not(lastimage), 0, $.extend(hidden, { immediateRender: false }), duration, duration)
              // add an 'empty' set after lastimage is made visible - this adds padding at the end of the timeline so lastimage is displayed for the correct duration before the repeat
              .set({}, {}, "+="+duration);
        }
        
      }
    });
  };
}(jQuery));
              
            
!
999px

Console