0

我注意到在使用 jQuery.fn.animate() 时,我可以以任何顺序将部分或全部参数(用于动画、回调函数、缓动和持续时间的 css)传递给函数。

查看函数源代码,它的开头是这样的:

function (prop, speed, easing, callback) {
    var empty = jQuery.isEmptyObject(prop),
        optall = jQuery.speed(speed, easing, callback),
.
.
.

然后它开始实际处理传递的信息。所以,显然 css 属性对象必须在链中的第一个 - 否则它会破坏函数(或者是它?)。但是再看看jQuery.speed:

function (speed, easing, fn) {
    var opt = speed && typeof speed === "object" ? jQuery.extend({},
    speed) : {
        complete: fn || !fn && easing || jQuery.isFunction(speed) && speed,
        duration: speed,
        easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
    };
.
.
.

显然,这就是魔法的所在。但是 jQuery 减少括号和大括号的方法让我很难分解所有这些条件。你能简化 jQuery.speed 函数吗?谢谢。

4

1 回答 1

1

以更全面的方式编写,这就是 jQuery 所做的:

function (speed, easing, fn) {
    var opt;
    if (speed && typeof speed === "object") {
        opt = jQuery.extend({}, speed);
    }
    else {
        var complete_p,
            duration_p = speed,
            easing_p;

        // Find out what's the "complete" property
        if (fn) complete_p = fn;
        else {
            if (easing) {
                complete_p = easing;
            }
            else {
                if (jQuery.isFunction(speed)) {
                    complete_p = speed;
                }
            }
        }

        // Find out what's the "easing" property
        if (fn) {
            easing_p = easing;
        }
        else {
            if (easing) {
                if (!jQuery.isFunction(easing)) {
                    easing_p = easing;
                }
            }
        }

        opt = {
            complete: complete_p,
            duration: duration_p,
            easing: easing_p
        };
    }
.
.
.

所以...是的,它正在做一些检查以允许您更改订单。不过,我不会依赖它。

于 2013-01-31T09:48:16.750 回答