Skip to content

Instantly share code, notes, and snippets.

@iammerrick
Forked from ryanflorence/gist:1055239
Created June 29, 2011 23:12
Show Gist options
  • Save iammerrick/1055244 to your computer and use it in GitHub Desktop.
Save iammerrick/1055244 to your computer and use it in GitHub Desktop.
$.fn.sprite = function(options){
var defaults = {
frames: 10,
inverse: false,
size: 20,
duration: 400
}
, options = $.extend({}, defaults, options)
, $el = $(this)
, currentFrame = 1
, fps = options.duration / frames
, positions = ($(this).css('background-position').split(' ') || ' ')
, top = positions[1]
, left = positions[0]
, backgroundPosition;
function animate(){
if(options.inverse){
top = parseInt(top) - options.size;
} else{
top = parseInt(top) + options.size;
}
$el.css('background-position', left+' '+top+'px');
currentFrame++;
if(currentFrame < options.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