Skip to content

Instantly share code, notes, and snippets.

@iammerrick
Created June 29, 2011 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iammerrick/1055195 to your computer and use it in GitHub Desktop.
Save iammerrick/1055195 to your computer and use it in GitHub Desktop.
$.fn.sprite = function(options){
var $el = $(this)
, frames = options.frames
, inverse = options.inverse
, currentFrame = 1
, size = options.size
, fps = options.duration / frames
, positions = ($(this).css('background-position').split(' ') || ' ')
, top = positions[1]
, left = positions[0]
, backgroundPosition;
function animate(){
if(inverse){
top = parseInt(top) - size;
} else{
top = parseInt(top) + size;
}
$el.css('background-position', left+' '+top+'px');
currentFrame++;
if(currentFrame < frames){
setTimeout(animate, fps);
}
}
setTimeout(animate , fps);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment