0

我正在尝试在 javascript 中构建一个简单的 Tween 类。代码如下所示:

function Tween(object, parameters) {      // constructor-class def.
    this.object = object;
    this.parameters = parameters;
    this.isRunning = true;
    this.frameRate = some value;
    this.timeElapsed = some value;
    this.duration = some value;
}

Tween.prototype.drawFrame = function () {
    var self = this;
    this.nextFrame();
    if (this.isRunning)
        setTimeout( function () { self.drawFrame() } , 1000 / frameRate);
}

Tween.prototype.nextFrame = function () {
    if (this.timeElapsed < this.duration) {
                // calculate and update the parameters of the object
    } else
        this.isRunning = false;
}

Tween.prototype.start = function () {
    this.isRunning = true;
    this.drawFrame();
}

然后由另一个脚本调用,比如 main.js :

window.onload = init;

init = function() {

var tweenDiv = document.createElement('div');
tweenDiv.id = 'someDiv';
document.body.appendChild(tweenDiv);
var myTween = new Tween(document.getElementById('someDiv').style, 'left', parameters);
myTween.start();

}

不幸的是,这不起作用。如果我使用 Google Chrome 开发人员工具检查我的网站,someDiv 的左样式属性设置为参数中设置的像素数。但它不会在屏幕上移动。

有人知道我在做什么错吗?为什么不重绘 someDiv?

非常感谢

4

1 回答 1

1

您必须将position样式设置为absolutefixedrelative使left样式生效。

于 2011-04-27T20:10:02.427 回答