JavaScript的流畅的动画从X,Y为X1,Y1 [英] javascript smooth animation from X,Y to X1,Y1

查看:126
本文介绍了JavaScript的流畅的动画从X,Y为X1,Y1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从sloothly实际X,Y位置X1,Y1移动图像(或要素)。

I'd like to sloothly move an image (or an element) from its actual X, Y location to X1, Y1.

当X和X1 距离等于到Y和Y1它很容易的。
但是,如果X有什么区别说100像素和Y差异是273px?

When the distance between X and X1 is equal to that between Y and Y1 its easy. But what if the X difference is say 100px and Y diff is 273px?

作为新的JavaScript,我不想重新发明轮子!
此外,由于我学习,我不想使用jQuery或喜欢。我想纯JavaScript。

Being new to Javascript, I don't want to re-invent the wheel! Besides, since I'm learning, I do NOT want to use jQuery or the likes. I want pure javascript.

请用简单的脚本提供: - )

Please supply with simple script :-)

推荐答案

解决方法一:

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 );
}

现场演示: http://jsfiddle.net/qEVVT/1/

这篇关于JavaScript的流畅的动画从X,Y为X1,Y1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆