Useful jQuery Plugin of the Day: Waypoints

At Cheezburger, my team is currently working on a sharing widget for our recently released list post type. If you take a look at this list, you will see our MVP of the idea. You will notice a share box in the left gutter that appears to float in a fixed position while you scroll through the items in the list giving you the opportunity to share at any point.

We are using an awesome little jQuery plugin called Waypoints to accomplish this. Waypoints makes it easy to execute a function whenever you scroll to an element. Our implementation looks like this.

define(['jquery', 'mods/device', 'libs/waypoints'], function($, device) {
    var f = {};

    f.listen = function () {
        if (device.is.desktop) {
            $('.js-footer').waypoint(f.affix, { offset: '75%' });
        }
        return this;
    };

    f.affix = function (event, direction) {
        event.stopPropagation();
        $('#js-share-menu-wrap').toggleClass('is-fixed');
        return this;
    };

    return f;
});

In this code we are attaching our waypoint to the footer of of the page. When the footer scrolls into the window viewport the affix method gets called. The affix method simply toggles the is fixed class on the share widget. We are also passing in an options object that tells the waypoint to trigger when the footer scrolls passed the point 75% from the top of the viewport.

In our next iteration, we are planning to change the way the share button behaves as you scroll through the list. The idea being when you click the button while it is within the area of the third list item, the image and title used to share the list on Facebook will be the third list items information. So while you scroll though the list we are updating the share buttons shared information.

We have not implemented this functionality yet, but yesterday I did a spike to figure out how we could accomplish it. And once again, Waypoints made the task easy.

define(['jquery', 'mods/device', 'libs/waypoints'], function($, device) {
    var f = {};

    f.listen = function () {
        if (device.is.desktop) {
            $('.actions').waypoint(f.affix, { offset: '75%' });
            $('.list-asset-item').waypoint(f.onSwitch, { offset: $('#js-share-menu-wrap').offset().top });
        }

        return this;
    };

    f.affix = function (event, direction) {
        event.stopPropagation();
        $('#js-share-menu-wrap').toggleClass('is-fixed');
        return this;
    };

    f.onSwitch = function(event, direction) {
        var item = $(this);
        console.log(direction);
        console.log(item.index());
        console.log($('h2.title', item).text());
        console.log($('div.post-description', item).text());
    };

    return f;
});

In this code I am attaching a waypoint to each list asset item. But this time I am setting the offset to the fixed pixel position of the top of the share widget. This allows me to trigger the onSwitch function as the top of the list asset item crosses the top of the share widget. The handler is scoped correctly for the list asset item so I can easily grab it’s index, title and description. Waypoints will even tell me which direction the user is scrolling.

Here is a quick video showing this code in action.

Written on March 12, 2013