首先,你的函数可以有三种结果:什么都不做,调用this.close()或调用this.open()。所以理想情况下,结果函数将只有一个 if 语句来确定使用哪个结果。
下一步是将所有布尔代码提取到变量中。例如var leftPastCenter = this.content.getBoundingClientRect().left > viewport / 2。
最后,使用布尔逻辑逐步简化。
这是我的做法:
首先,提取所有布尔变量:
function () {
    var duration = +new Date() - start.time,
      isPastHalf = Number(duration) < 250 && Math.abs(delta.x) > 20 || Math.abs(delta.x) > viewport / 2,
      direction = delta.x < 0,
      leftPastCenter = this.content.getBoundingClientRect().left > viewport / 2,
      positiveDelta = this.isEmpty(delta) || delta.x > 0,
      isPulled = pulled === true; // I'll assume the test is needed rather than just using pulled.
    if (!isScrolling) {
        if (isPastHalf) {
            if (direction) {
                this.close();
            } else {
                if (leftPastCenter && isPulled) {
                    this.close();
                    return;
                }
                this.open();
            }
        } else {
            if (leftPastCenter) {
                if (positiveDelta) {
                    this.close();
                    return;
                }
                this.open();
                return;
            }
            this.close();
        }
    }
}
最容易摆脱的部分是意识到如果isScrolling是真的,什么都不会发生。这立即摆脱了一层嵌套:
    // above same
    if (isScrolling) { return; }
    if (isPastHalf) {
        if (direction) {
            this.close();
        } else {
            if (leftPastCenter && isPulled) {
                this.close();
                return;
            }
            this.open();
        }
    } else {
        if (leftPastCenter) {
            if (positiveDelta) {
                this.close();
                return;
            }
            this.open();
            return;
        }
        this.close();
    }
}
现在看看this.open()被调用的案例。如果isPastHalf为真,this.open()则仅在!direction和时调用!(leftPastCenter && isPulled)。如果isPastHalf为假,则this.open()仅在leftPastCenter和时调用!positiveDelta:
    // above same
    if (isScrolling) { return; }
    if (isPastHalf) {
        if (!direction && !(leftPastCenter && isPulled)) {
            this.open();
        } else {
            this.close();
        }
    } else {
        if (leftPastCenter && !positiveDelta) {
            this.open();
        } else {
            this.close();
        }
    }
翻转 ifs(所以this.close()首先出现),使代码更整洁,并给出我的最终版本:
    function () {
    var duration = +new Date() - start.time,
      isPastHalf = Number(duration) < 250 && Math.abs(delta.x) > 20 || Math.abs(delta.x) > viewport / 2,
      direction = delta.x < 0,
      leftPastCenter = this.content.getBoundingClientRect().left > viewport / 2,
      positiveDelta = this.isEmpty(delta) || delta.x > 0,
      isPulled = pulled === true; // I'll assume the test is needed rather than just using pulled.
    if (isScrolling) { return; }
    if (isPastHalf) {
        if (direction || (leftPastCenter && isPulled)) {
            this.close();
        } else {
            this.open();
        }
    } else {
        if (!leftPastCenter || positiveDelta) {
            this.close();
        } else {
            this.open();
        }
    }
}
在不了解您的代码库的情况下,我很难做更多事情。需要注意的一件事是direction,我的新变量positiveDelta几乎相同 - 您可以删除positiveDelta并仅使用direction. 此外,direction对于布尔值来说,这不是一个好名字,类似的东西movingLeft会更好。