如何在Javascript中动画图像对象?

时间:2013-10-29 15:32:18

标签: javascript jquery jquery-animate

我遇到了动画图像对象的问题。这就是我制作图像的方式:

var paddleUpgradeImage = new Image();
paddleUpgradeImage.src = "images/expand.gif";

function makeEaseOut(delta) {  
    return function(progress) {
        return 1 - delta(1 - progress);
    };
}

function bounce(progress) {
    for(var a = 0, b = 1, result; 1; a += b, b /= 2) {
        if (progress >= (7 - 4 * a) / 11) {
            return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2) + Math.pow(b, 2)
        }
    }
}

function quad(progress) {
    return Math.pow(progress, 2)
}

function drawMovement() {
    paddleUpgradeImage.animate({
        delay: 20,
        duration: 3000,
        delta: makeEaseOut(bounce), 
        step: function(delta) {
            //img.style.top = height*delta + 'px';
            upgradesY[0] = 400*delta;
        }
    });

    paddleUpgradeImage.animate({
        delay: 20,
        duration: 3000, 
        delta: makeEaseOut(quad),
        step: function(delta) {
            upgradesX[0] = 200*delta;
            //img.style.left = width*delta  + "px";
        }
    });
}

UpgradesX和升级Y是保持图片位置的数组。 它给了我这个错误:

Uncaught TypeError: Object #<HTMLImageElement> has no method 'animate' 

如何解决此问题并使我的图像移动?

1 个答案:

答案 0 :(得分:2)

只需调用$(或jQuery,如果您处于无冲突模式),将DOM元素作为参数传递。

var x = new Image()
x.src = 'y.gif'
$(x).animate({ /* ... */ })