首页 > 解决方案 > `this.previous().previous().element == this.element`?

问题描述

我正在将我的代码从 jquery-waypoints 2.x 升级到 4.x,并发现与this.previous(). 我发现在正确时间触发的处理程序中this.element是正确的。但是,this.previous().element用路标指向 DOM 中的下一个元素,并this.previous().previous().element指向与this.element. 我可以在代码中设置一个断点并对其进行验证this.previous().previous().element == this.element

我不得不想象我做错了什么,或者这是一个错误。我在 v4.0.1 上,我使用的是 jQuery 版本。我像这样绑定我的航点:

$selector.waypoints(
    function goingUp(direction) {
        console.log(this.previous().previous().element == this.element); // true
        console.log(this.next().previous().element == this.element); // true
    }, {
        offset: offsetCalculator(this),
    });

标签: jquery-waypoints

解决方案


问题原来是航路点的分组。该.previous()航路点(不计入分组)在文档顺序中出现的时间晚于当前航路点。一旦我将组添加到航点,.previous()并按.next()预期工作。


推荐阅读