JSFiddle - React, Tailwind, and code Playground
HTML
<div id="wrapper">
<div id="block"></div>
</div>
CSS
#wrapper {
width: 400px;
height: 300px;
background: url("https://dl.dropbox.com/u/2787908/background.png") no-repeat 0 0;
position: relative;
margin: 15px;
}
#block {
width: 400px;
height: 100px;
background: #99f;
position: absolute;
top: 100px;
}
JavaScript
$(document).ready(function(){
//$("#wrapper").animate({ backgroundPosition:"(0 -100px)" }, 20000, "linear");
$("#block").animate({ top: 0 }, {
duration: 20000,
easing: "linear",
step: function(now, fx){
console.log("0 "+(parseInt($(this).css("top"), 10)-100)+"px");
$("#wrapper").css("background-position", "0 "+(parseInt($(this).css("top"), 10)-99)+"px");
}
});
});
/* background position animation plugin */
(function($) {
if(!document.defaultView || !document.defaultView.getComputedStyle){
var oldCurCSS = jQuery.curCSS;
jQuery.curCSS = function(elem, name, force){
if(name === 'background-position'){
name = 'backgroundPosition';
}
if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
return oldCurCSS.apply(this, arguments);
}
var style = elem.style;
if ( !force && style && style[ name ] ){
return style[ name ];
}
return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
};
}
var oldAnim = $.fn.animate;
$.fn.animate = function(prop){
if('background-position' in prop){
prop.backgroundPosition = prop['background-position'];
delete prop['background-position'];
}
if('backgroundPosition' in prop){
prop.backgroundPosition = '('+ prop.backgroundPosition + ')';
}
return oldAnim.apply(this, arguments);
};
function toArray(strg){
strg = strg.replace(/left|top/g,'0px');
strg = strg.replace(/right|bottom/g,'100%');
strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
return...