Did*_*evy 6 javascript animation smooth image
我想将图像(或元素)从其实际的X,Y位置平滑地移动到X1,Y1.
当X和X1之间的距离等于 Y和Y1之间的距离时,它很容易.但是,如果X差异是100px而Y差异是273px怎么办?
作为Javascript的新手,我不想重新发明轮子!此外,因为我正在学习,我不想使用jQuery或类似的东西.我想要纯粹的javascript.
请提供简单的脚本:-)
一个解决方案
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 );
}
Run Code Online (Sandbox Code Playgroud)
现场演示: http ://jsfiddle.net/qEVVT/1/