Skip to content

Instantly share code, notes, and snippets.

@cemerson
Created July 7, 2019 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemerson/1eccc45e93d9d2ae43c3ba0056c36d3e to your computer and use it in GitHub Desktop.
Save cemerson/1eccc45e93d9d2ae43c3ba0056c36d3e to your computer and use it in GitHub Desktop.
jQuery Animate Background Postion (CSS)
// https://stackoverflow.com/questions/12340130/jquery-animate-background-image-on-y-axis
$.fn.animateBG = function(x, y, speed) {
var pos = this.css('background-position').split(' ');
this.x = pos[0].split('px')[0] || 0;
this.y = pos[1].split('px')[0] || 0;
$.Animation( this, {
x: x,
y: y
}, {
duration: speed
}).progress(function(e) {
this.css('background-position', e.tweens[0].now+'px '+e.tweens[1].now+'px');
});
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment