JSFiddle - React, Tailwind, and code Playground
HTML
<img id="test" src="http://placekitten.com/100/100">
<button title="100 100">Translate to (100, 100)</button>
<button title="0 200">Translate to (0, 200)</button>
<button title="200 50">Translate to (200, 50)</button>
CSS
#test { position:absolute; left:100px; top:50px; }
JavaScript
var img = document.getElementById( 'test' );
function translate( elem, x, y ) {
var left = parseInt( css( elem, 'left' ), 10 ),
top = parseInt( css( elem, 'top' ), 10 ),
dx = left - x,
dy = top - y,
i = 1,
count = 20,
delay = 20;
function loop() {
if ( i >= count ) { return; }
i += 1;
elem.style.left = ( left - ( dx * i / count ) ).toFixed( 0 ) + 'px';
elem.style.top = ( top - ( dy * i / count ) ).toFixed( 0 ) + 'px';
setTimeout( loop, delay );
}
loop();
}
function css( element, property ) {
return window.getComputedStyle( element, null ).getPropertyValue( property );
}
window.onclick = function ( e ) {
var arr;
if ( e.target.nodeName === 'BUTTON' ) {
arr = e.target.title.split( ' ' );
translate( img, +arr[0], +arr[1] );
}
};