淡入和淡出功能不起作用

fadeIn and fadeOut functions not working

本文关键字:不起作用 功能 淡出 淡入      更新时间:2023-09-26

我正在使用纯javascript处理淡入和淡出函数,这是代码:

(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;
})()

我使用以下代码来激活该功能:

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

但是当我加载页面时,出现激活码错误。

有谁知道我做错了什么?

非常感谢。

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

  1. 正如理查德·道尔顿所注意到的,用fx而不是FX来称呼对象。
  2. 为所有选项
  3. 添加默认值,以防未提供选项(示例代码会引发错误,因为未定义options.complete(。例:

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

    element.style.opacity = 0;
    element.style.visibility = "visible";
    element.style.display = "block";
    
  5. 检查是否需要操作(例如,尽管元素已经可见,但仍调用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(...但我希望我为你指出了正确的方向。:)