Waypoints is a jQuery plugin that makes it easy to execute a function whenever you scroll to an element.
$('.thing').waypoint(function(direction) {
alert('Top of thing hit top of viewport.');
});
Waypoints can now be created for horizontal scrolling. Just pass horizontal:true to your waypoint options.
One HTML element can now have multiple Waypoints attached to it, each with its own offset and handler.
The most common uses for Waypoints are now even easier to implement with shortcut methods.
A new handler signature, added introspection methods, and more. Be sure to check the new docs and the changelog.
First thing's first, to get started with Waypoints, download the plugin and include it after you've included jQuery.
<script src="/path/to/jquery.min.js"></script>
<script src="/path/to/waypoints.min.js"></script>
You'll now have access to the .waypoint method on your jQuery objects.
The simplest way to use Waypoints is to call .waypoint and pass in a function.
$('#example-basic').waypoint(function() {
notify('Basic example callback triggered.');
});
This example causes a notification whenever the top of #example-basic hits the top of the viewport. The callback triggers when you pass this point both scrolling up and scrolling down.
More often than not we want to perform different actions depending on which direction the user is scrolling. Waypoints passes this direction as a parameter to the callback function.
$('#example-direction').waypoint(function(direction) {
notify('Direction example triggered scrolling ' + direction);
});
Here the notification reads "Direction example triggered scrolling down" or "Direction example triggered scrolling up".
What if you want your waypoint to trigger in a location other than when the top of your element hits the top of the viewport? The waypoint function takes a second argument, an options object. One of the most useful options is offset, which tells Waypoints how far from the top of the window the callback should fire. This can be expressed in pixels…
$('#example-offset-pixels').waypoint(function() {
notify('100 pixels from the top');
}, { offset: 100 });
…as a percentage of the viewport's height…
$('#example-offset-percent').waypoint(function() {
notify('25% from the top');
}, { offset: '25%' });
…or as a function. The function needs to return a number. The following example will fire when the bottom of the element hits the top of the viewport.
$('#example-offset-function').waypoint(function() {
notify('Element bottom hit window top');
}, {
offset: function() {
return -$(this).height();
}
});
Let's say you have a div with overflow:scroll, and you want a waypoint inside of this scrollable element. The context option lets you do this. Scroll the box below.
$('#example-context').waypoint(function() {
notify('Hit top of context');
}, { context: '.example-scroll-div' });
Not all sites scroll vertically. You can create a waypoint on the horizontal axis with the horizontal option. This project page uses horizontal waypoints when the user navigates between high-level sections.
$('.panel').waypoint(function(direction) {
$body.toggleClass(this.id + '-visible', direction === 'right');
}, {
offset: '50%',
horizontal: true
});
As you can see, when horizontal is set to true the direction passed to the callback changes from "down" and "up" to "right" and "left".
You can also create multiple waypoints on the same HTML element. Each waypoint can have its own options. The horizontal example above isn't the only waypoint created on the .panel elements.
$('.panel').waypoint(function(direction) {
$body.toggleClass(this.id + '-visible', direction === 'left');
}, {
offset: '-50%',
horizontal: true
});
CoffeeScript authors may find passing a function followed by an options object to be a bit clumsy. There is another way to pass the waypoints callback, using the handler option. If you provide a handler option, you only need to pass the options object.
/* In JavaScript */
$('#example-handler').waypoint({
handler: function() {
notify('Handler option used');
},
offset: '50%'
});
# In CoffeeScript
$('#example-handler').waypoint
handler: ->
notify 'Handler option used'
offset: '50%'
jQuery Waypoints extensions that simplify some common UI patterns.
Sticky Elements Infinite ScrollCalling the waypoint function on a set of elements registers them as waypoints. You can pass in a callback function…
$('.thing').waypoint(function(direction) {
// do stuff
});
…an options object…
$('.thing').waypoint({
handler: function(direction) {
// do stuff
}
});
…or both…
$('.thing').waypoint(function(direction) {
// do stuff
}, { offset: 50 })
…but you must pass something. Attempting to call waypoint without parameters or without a callback function will result in an error.
$('.thing').waypoint(); // error
The waypoint callback function takes one parameter, direction. For vertical waypoints direction may be 'up' or 'down'. For horizontal waypoints, 'left' or 'right'.
$('.thing').waypoint(function(direction) {
alert(direction); // up, down, left, or right
});
The options object lets you define where in the viewport to trigger the waypoint, whether the waypoint should be on the horizontal axis, and much more.
$.fn.waypoint.defaults = {
context: window,
continuous: true,
enabled: true,
horizontal: false,
offset: 0,
triggerOnce: false
}
Temporarily disables the waypoint callback function from firing. The waypoint can be re-enabled by calling enable.
$('.thing').waypoint('disable');
Destroys all waypoints associated with these elements.
$('.thing').waypoint('destroy');
Returns a jQuery object containing the previous waypoint, as measured by the waypoints' trigger locations.
$('#thing').waypoint(...);
$('#thing-farther-down').waypoint(...);
$('#thing-farther-down').waypoint('prev');
// returns #thing jQuery object
Returns a jQuery object containing the previous waypoint, as measured by the waypoints' trigger locations.
$('#thing').waypoint(...);
$('#thing-farther-down').waypoint(...);
$('#thing').waypoint('next');
// returns #thing-farther-down jQuery object
Returns an object with two keys, vertical and horizontal. Each of these key values is an HTMLElement array containing all waypoint elements on that axis. If contextSelector is provided, it only returns elements with a context matching that context selector. If no parameter is supplied, all waypoint elements are returned.
$('.thing').waypoint(...);
$.waypoints();
// returns {
// vertical: [(all .thing elements)]
// horizontal: []
// }
This will force a recalculation of each waypoint’s trigger point based on its offset option. This is called automatically whenever the window is resized or new waypoints are added. If your project is changing the DOM or page layout without doing one of these things, you may want to manually call it.
$.waypoints('refresh');
Return the height of the viewport, adjusting for inconsistencies that come with calling $(window).height() in iOS. When writing offset functions that use the window's height, using this is recommended.
$.waypoints('viewportHeight');
Returns an array of HTMLElements. This array contains all waypoint elements with trigger points that lie above the current scroll position of the contextSelector element.
$('.things').waypoint(...);
$.waypoints('above');
// returns all "things" above current scroll position
Returns an array of HTMLElements. This array contains all waypoint elements with trigger points that lie below the current scroll position of the contextSelector element.
$('.things').waypoint(...);
$.waypoints('below');
// returns all "things" below current scroll position
Returns an array of HTMLElements. This array contains all waypoint elements with trigger points that lie to the left of the current scroll position of the contextSelector element.
$('.things').waypoint(...);
$.waypoints('left');
// returns all "things" left of current scroll position
Returns an array of HTMLElements. This array contains all waypoint elements with trigger points that lie to the right of the current scroll position of the contextSelector element.
$('.things').waypoint(...);
$.waypoints('right');
// returns all "things" right of current scroll position
Disables all waypoints.
Re-enables all waypoints.
Destroys all waypoints.
Adds method to the .waypoint namespace under the name key. Shortcuts use this to add their methods to jQuery Waypoints.
$.waypoints('extendFn', 'alert', function(msg) {
alert(msg + this.id);
});
$('#thing').waypoint('alert', 'Foo: ');
// Alerts "Foo: thing"