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='spriteslider' id='spriteslider'>
  <div id='spritetarget'></div>
</div>

<p>Drag the box left/right to control the sprite's position.</p>
              
            
!

CSS

              
                body {
  text-align: center;
  font: normal 12px sans-serif;
  background: #000000;
  color: #91E600;
}
.spriteslider {
  margin: 20px auto;
  padding: 60px;    
  width: 128px;
  background: #FCFEFC;
  border-radius: 5px;
}
#spritetarget {
  width: 128px;
  height: 128px;
  background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/heart.png); /* horizontal spritesheet - image from http://preloaders.net */
  background-repeat: repeat-x;
}
              
            
!

JS

              
                // use a dummy element as the target since we don't really need anything visual to drag around
var spriteslider = document.createElement('div');
// the dummy element needs to be added to the DOM in latest Draggable
document.body.appendChild(spriteslider);

// attach all vars to the dummy element so globals aren't needed:
// element that initiates dragging
spriteslider.slider = document.getElementById('spriteslider');
// element with the sprite sheet to control
spriteslider.sprite = document.getElementById('spritetarget');
// size of each sprite frame
spriteslider.spritesize = 128;
// number of sprite frames - used to keep multiplier in a reasonable range
spriteslider.spritecount = 20;
// pixels to drag per sprite update
spriteslider.pixelsperincrement = 25;
// values used by the sprite changer
spriteslider.multiplier = spriteslider.lastmultiplier = 0;


// create the 360 degree slider
Draggable.create(spriteslider, {
  	type: 'x',
  trigger: spriteslider.slider,
  // don't need to slow down as you drag further away
	  edgeResistance: 0,
  cursor: 'e-resize',
  throwProps: true,
  onDrag: function() {
    // there is an extra drag event fired on mouse up that has x = 0, so check this.isDragging to skip that last one
    if (this.isDragging) {
      var t = this.target; // the dummy div
      t.multiplier = (Math.floor(this.x / t.pixelsperincrement) + t.lastmultiplier) % t.spritecount;
      TweenLite.set(t.sprite, { backgroundPosition: (-t.multiplier * t.spritesize) + "px 0"});
    }
  },
  onThrowUpdate: function() {
    var t = this.target; // the dummy div
    t.multiplier = (Math.floor(this.x / t.pixelsperincrement) + t.lastmultiplier) % t.spritecount;
    TweenLite.set(t.sprite, { backgroundPosition: (-t.multiplier * t.spritesize) + "px 0"});
  },
	  onThrowComplete: function()	{
    // saves the current multiplier, and keeps multiplier small so that backgroundPosition doesn't end up too large for the browser to render
    var t = this.target; // the dummy div
    t.lastmultiplier = t.multiplier % t.spritecount;
  }
});
              
            
!
999px

Console