0

所以我正在做一个小项目,主要是为了帮助我学习更好的javascript,但我遇到了一个小问题。我可以很好地制作动画,但我的问题是.. 我试图找出计算像素变化量以移动动画的每一步所需的数学。所以说我的 div 是 450px 宽,我说的是 animate({ width : 600, duration : 2000 })。这留下了 150 像素的总移动量,但是如果我每 3 毫秒调用一次 setTimeout 函数,我为每帧做多少移动。我知道解决方案可能很简单,但出于某种原因,也许只是因为我在写这整件事时一直在敲墙,现在我想不出足够的答案。如果有人需要,我可以展示代码示例。提前感谢您的帮助。

animate : function(params, duration) {
    if(!params) {
        return this;
    } else {
        this.current = this._defCurrent;
        console.log(this.current);
        var time = (duration) ? duration : this.slow;
        var target;
        for(var index in params) {
            if(index == 'queue') {
                if(params[index]){
                    this.animQueue = true;
                } else {
                    this.animQueue = false;
                }
            } else {
                var current = parseFloat(this.getStyle(index));
                if(current < params[index]) {
                    target = params[index] - current;
                    animDirection = '+';
                } else {
                    target = current - params[index];
                    animDirection = '-';
                }
                totalMovement = target / time;
                animObj = {type : index, target : target, move : totalMovement, direction : animDirection, duration : time};
                this.setAnimQueue(animObj);
            }
        }
    }
    this.setTheTimeout();
},
setAnimQueue : function(obj) {
    var that = this;
    for(var i = 0, amt = (obj.duration * this.fps); i < amt; i++) {
        var fun = this.wrapFunction(that.doAnim, this, [obj.type, obj.move, obj.direction]);
        this.queue.push(fun);
    }
},
setTheTimeout : function() {
    var that = this;
    this.interValAmt = setInterval(function() {
        that.deQueue()
    }, this.fps);
},
doAnim : function(type, amount, direction) {
    var totalAmount = eval(parseFloat(this[type]()) + direction + amount);
    if(this.elem.style[this.toCamelCase(type)]) {
        this[type](totalAmount);
    } else {
        this[type](totalAmount)
    }
    return this;
}
4

1 回答 1

1

你有距离,你有持续时间。将它们分开可以提供速度。只需将速度乘以时间增量即可得到距离增量。

于 2012-07-31T04:29:36.937 回答