8

I am working on fadein and fadeout functions using pure javascript, here is the code:

(function() {
    var fx = {
        easing: {
            linear: function(progress) {
                return progress;
            },
            quadratic: function(progress) {
                return Math.pow(progress, 2);
            },
            swing: function(progress) {
                return 0.5 - Math.cos(progress * Math.PI) / 2;
            },
            circ: function(progress) {
                return 1 - Math.sin(Math.acos(progress));
            },
            back: function(progress, x) {
                return Math.pow(progress, 2) * ((x + 1) * progress - x);
            },
            bounce: function(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);
                    }
                }
            },
            elastic: function(progress, x) {
                return Math.pow(2, 10 * (progress - 1)) * Math.cos(20 * Math.PI * x / 3 * progress);
            }
        },
        animate: function(options) {
            var start = new Date;
            var id = setInterval(function() {
                var timePassed = new Date - start;
                var progress = timePassed / options.duration;
                if (progress > 1) {
                    progress = 1;
                }
                options.progress = progress;
                var delta = options.delta(progress);
                options.step(delta);
                if (progress == 1) {
                    clearInterval(id);
                    options.complete();
                }
            }, options.delay || 10);
        },
        fadeOut: function(element, options) {
            var to = 1;
            this.animate({
                duration: options.duration,
                delta: function(progress) {
                    progress = this.progress;
                    return fx.easing.swing(progress);
                },
                complete: options.complete,
                step: function(delta) {
                    element.style.opacity = to - delta;
                }
            });
        },
        fadeIn: function(element, options) {
            var to = 0;
            this.animate({
                duration: options.duration,
                delta: function(progress) {
                    progress = this.progress;
                    return fx.easing.swing(progress);
                },
                complete: options.complete,
                step: function(delta) {
                    element.style.opacity = to + delta;
                }
            });
        }
    };
    window.fx = fx;
})()

I am using the following code to activate the function:

document.getElementById('in').addEventListener('click', function() {
    FX.fadeIn(document.getElementById('type'), {
        duration: 2000,
    });
}, false);

But when I load the page I get an error with the activation code.

Does anyone know what I have done wrong?

Thank you so much.

4

1 回答 1

11

通过一些调整,您的代码应该可以正常工作......

  1. 正如Richard Dalton注意到的,用fxnot调用对象FX
  2. 为所有选项添加默认值,以防未提供选项(您的示例代码由于options.complete未定义而引发错误)。例子:

    this.animate({
        duration: options.duration || 1000,
        ...
        complete: options.complete || function() { },
        ...
    });
    
  3. 在动画之前调整样式(想象一个元素使用display: none而不是隐藏opacity: 0)......示例(对于fadeIn):

    element.style.opacity = 0;
    element.style.visibility = "visible";
    element.style.display = "block";
    
  4. 检查是否需要执行操作(例如,fadeIn尽管元素已经可见,但仍调用):

    isVisible: function(element) {
        return element.style.display !== "none" && 
                element.style.visibility !== "hidden" && 
                element.style.opacity !== "0";
    }
    
    // In fadeIn:
    if (!this.isVisible(element)) {
        ...
    }
    

这是带有示例的更正代码:JSFiddle

当然可以进一步调整这一点,处理一些极端情况(例如,如果fadeIn在元素当前淡出时调用 会发生什么?),我很确定它不适用于所有浏览器(旧的 IE)...但我希望我为您指明了正确的方向。:)

于 2013-10-27T10:22:13.353 回答