jquery-waypoints

`this.previous().previous().element == this.element`?


I'm upgrading my code from jquery-waypoints 2.x to 4.x and have identified strange behavior related to this.previous(). I find that in a handler that was triggered at the correct time, this.element is correct. However, this.previous().element points to the next element in the DOM with a waypoint and this.previous().previous().element points to the same element as this.element. I can set a break point in the code and have validated that this.previous().previous().element == this.element.

I have to imagine I'm doing something very wrong, or this is a bug. I'm on v4.0.1 and I'm using the jQuery version. I'm binding my waypoints like so:

$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),
    });

Solution

  • The problem turned out to be the grouping of waypoints. The .previous() waypoint, not counting for grouping, appeared later in document order than the current waypoint. Once I added groups to the waypoints, .previous() and .next() worked as expected.