jQuery Waypoints
Waypoints is a small jQuery plugin that makes it easy to execute a function whenever you scroll to an element.
$('.entry').waypoint(function() {
alert('You have scrolled to an entry.');
});
Waypoints makes a solid base for modern UI patterns that depend on a user’s scroll position on the page. Take a look at a few examples.
1.6kB min+gzip, 3.9kB minified, 21.2kB full source
Full Documentation
$.fn.waypoint
The waypoint namespace extension to the jQuery effin’ object. This is the meat of the plugin, calling .waypoint on one or more elements and registering handlers for when the elements have been reached.
.waypoint([handler], [options])
- handler — function, optional
- A callback function called when the user scrolls past the element. The function signature is
function(event, direction)whereeventis a standard jQuery Event Object anddirectionis a string, either'down'or'up'indicating which direction the user is scrolling. - options — object, optional
- A map of options to apply to this set of waypoints, including where on the browser window the waypoint is triggered. For a full list of options and their defaults, see $.fn.waypoint.defaults.
This is how you register an element as a waypoint. When the user scrolls past that element it triggers waypoint.reached, a custom event. Since the parameters for creating a waypoint are optional, we have a few different possible signatures. Let’s look at each of them.
someElements.waypoint();
Calling .waypoint with no parameters will register the elements as waypoints using the default options. The elements will fire the waypoint.reached event, but calling it in this way does not bind any handler to the event. You can bind to the event yourself, as with any other event, like so:
someElements.bind('waypoint.reached', function(event, direction) {
// make it rain
});
You will usually want to create a waypoint and immediately bind a function to waypoint.reached, and can do so by passing a handler as the first argument to .waypoint:
someElements.waypoint(function(event, direction) {
if (direction === 'down') {
// do this on the way down
}
else {
// do this on the way back up through the waypoint
}
});
This will still use the default options, which will trigger the waypoint when the top of the element hits the top of the window. We can pass .waypoint an options object to customize things:
someElements.waypoint(function(event, direction) {
// do something amazing
}, {
offset: '50%' // middle of the page
});
You can also pass just an options object.
someElements.waypoint({
offset: 100 // 100px from the top
});
This behaves like .waypoint(), in that it registers the elements as waypoints but binds no event handlers.
Calling .waypoint on an existing waypoint will extend the previous options. If the call includes a handler, it will be bound to waypoint.reached without unbinding any other handlers.
.waypoint('destroy')
Passing the string 'destroy' to .waypoint will unbind all waypoint.reached event handlers on those elements and unregisters them as waypoints.
.waypoint('remove')
Passing the string 'remove' to .waypoint unregisters the elements as waypoints and wipes any custom options, but leaves the waypoint.reached events bound. Calling .waypoint again in the future would reregister the waypoint and the old handlers would continue to work.
$.fn.waypoint.defaults
The default options object that is extended when calling .waypoint. It has the following properties:
- context
- string (| element | jQuery object if jQuery 1.6+)
- default: window
- The context defines which scrollable element the waypoint belongs to and acts within. The default, window, means the waypoint offset is calculated with relation to the whole viewport. You can set this to another element to use the waypoints within that element. Accepts a selector string, but if you use jQuery 1.6+ it also accepts a raw HTML element or jQuery object.
- continuous
- boolean
- default: true
- If true, and multiple waypoints are triggered in one scroll, this waypoint will trigger even if it is not the last waypoint reached. If false, it will only trigger if it is the last waypoint.
- handler
- function
- default: undefined
- An alternative way to bind functions to the waypoint without using the first argument of the waypoint function.
- offset
- number | string | function
- default: 0
- Determines how far the top of the element must be from the top of the browser window to trigger a waypoint. It can be a number, which is taken as a number of pixels, a string representing a percentage of the viewport height, or a function that will return a number of pixels.
- onlyOnScroll
- boolean
- default: false
- If true, this waypoint will not trigger if an offset change during a refresh causes it to pass the current scroll point.
- triggerOnce
- boolean
- default: false
- If true, the waypoint will be destroyed when triggered.
An offset of 250 would trigger the waypoint when the top of the element is 250px from the top of the viewport. Negative values for any offset work as you might expect. A value of -100 would trigger the waypoint when the element is 100px above the top of the window.
offset: '100%'
A string percentage will determine the pixel offset based on the height of the window. When resizing the window, this offset will automatically be recalculated without needing to call $.waypoints('refresh').
// The bottom of the element is in view
offset: function() {
return $.waypoints('viewportHeight') - $(this).outerHeight();
}
Offset can take a function, which must return a number of pixels from the top of the window. The this value will always refer to the raw HTML element of the waypoint. As with % values, functions are recalculated automatically when the window resizes. For more on recalculating offsets, see $.waypoints('refresh').
An offset value of 'bottom-in-view' will act as an alias for the function in the example above, as this is a common usage.
offset: 'bottom-in-view'
You can see this alias in use on the scroll analytics example page.
The triggerOnce flag, if true, will destroy the waypoint after the first trigger. This is just a shortcut for calling .waypoint('destroy') within the waypoint handler. This is useful in situations such as scroll analytics, where you only want to record an event once for each page visit.
The context option lets you use Waypoints within an element other than the window. You can define the context with a selector string and the waypoint will act within the nearest ancestor that matches this selector.
$('.something-scrollable .waypoint').waypoint({
context: '.something-scrollable'
});
You can see this in action on the dial controls example.
The handler option gives authors an alternative way to bind functions when creating a waypoint. In place of:
$('.item').waypoint(function(event, direction) {
// make things happen
});
You may instead write:
$('.item').waypoint({
handler: function(event, direction) {
// make things happen
}
});
$.waypoints
The waypoints namespace extension to the jQuery object houses methods and settings dealing with all waypoints that have been registered through fn.waypoint.$.waypoints()
Returns: jQuery
This will return a jQuery object with a collection of all registered waypoint elements.
$('.post').waypoint();
$('.ad-unit').waypoint(function(event, direction) {
// Passed an ad unit
});
console.log($.waypoints());
The example above would log a jQuery object containing all .post and .ad-unit elements.
$.waypoints('refresh')
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, new waypoints are added, or a waypoint’s options are modified. If your project is changing the DOM or page layout without doing one of these things, you may want to manually call this refresh.
$.waypoints('viewportHeight')
Returns: number
This will return the height of the viewport, adjusting for inconsistencies that come with calling $(window).height() in iOS. Recommended for use within any offset functions.
$.waypoints.settings
Settings object that determines some of the plugin’s behavior.
- resizeThrottle
- number
- default: 200
- For performance reasons, the refresh performed during window resizes is throttled. This value is the rate-limit in milliseconds between resize refreshes. For more information on throttling, check out Ben Alman’s throttle / debounce plugin.
- scrollThrottle
- number
- default: 100
- For performance reasons, checking for any crossed waypoints during the window scroll event is throttled. This value is the rate-limit in milliseconds between scroll checks. For more information on throttling, check out Ben Alman’s throttle / debounce plugin.
If you find something wrong with the plugin, the documentation, or wish to request a feature, let me know on the project’s issue page.