0
我試圖在javascript中構建一個簡單的補間類。該代碼讀取這樣的:javascript中的簡單補間類問題
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將其左側樣式屬性設置爲參數中設置的像素數。但它不會在屏幕上移動。
有人知道我在做什麼錯嗎?爲什麼不是某個DIV正在重繪?
非常感激
非常感謝!這工作! – Mansiemans 2011-04-28 08:33:01