The first thing you'll need to do is download Waypoints. The
lib/ directory contains builds for jQuery and Zepto, as well as a version with no framework dependencies. Choose the one that fits your needs and include it.
The rest of this guide and any code snippets on this site, unless otherwise noted, will be using the no-framework build. If you're interested in the goodies that come with jQuery and Zepto builds, take a look at the jQuery/Zepto guide. Code snippets for the no-framework build will work in other builds.
A Basic Waypoint
With Waypoints included, we now have access to a global
Waypoint class. We create waypoints by instantiating this class. When creating a new
Waypoint we must pass it an options object. There are many properties you can set on this options object, but two of them are required,
element tells Waypoints which DOM element's position to observe during scroll, and
handler is the function that will trigger when the top of that element hits the top of the viewport.
You may notice the basic example above triggers when we scroll through the waypoint both downwards and upwards. What if we want to perform different actions when scrolling up, or limit our handler to one direction? When a waypoint is triggered, the handler function is passed a
By default a waypoint triggers when the top of the element hits the top of the window. What if we want it to trigger when the element is 20px from the top instead?
offset option can take a variety of different values to help us control where a waypoint is triggered. For more information, check out the offset option documentation.
When we're inside the handler function the
this keyword is a reference to the Waypoint instance. We can use this object to access all the properties and methods in the API. One of the most useful properties is
element, the waypoint's DOM element.
This has been a very brief guide to the basics of Waypoints. The API documentation goes into more depth about all of the options, methods, and properties available to us. In addition to being a reference for more technically savvy developers, each of the API pages acts as a guide for how to use that feature. The docs are intended to be useful for beginners and experts alike.
If you already have a use for Waypoints in mind, take a moment to check out the Shortcuts section. These shortcut scripts take some of the most common uses of Waypoints and packages them into ready-made extensions.