Note: This library has been deprecated and the repo is no longer being maintained. See alternatives for other options.
This is a JavaScript library for scrollytelling, which is dynamically changing charts (or triggering whatever) as text scrolls into view. It implements best practices for scrollytelling, which means built-in keyboard shortcuts, no scrolljacking and reliable "sticky" behaviour.
TwoStep was developed at The Wall Street Journal and has been used in stories such as:
TwoStep is highly flexible, and can be used in range of designs.
- Basic working example
- One-column layout, with text scrolling over the top
- Two instances on the same page
- With swipe carousel on mobile
- With static charts on mobile
These demos are also meant as starting points for new projects.
-
Set up HTML and CSS as best fits your project. See source code from the demos above for inspiration.
-
In your JavaScript:
var ts = new TwoStep({
elements: document.querySelectorAll('.slide-item'),
onChange: function(event) {
console.log(event.index);
}
});
Only elements
is required. All others are optional.
elements
: Array/NodeList of DOM nodes (i.e. your narrative text).narrative
: Array of functions corresponding to elements. Each called with event object as argument.onChange
: Called when any/every element is activated. Called with event object as argument.stick
: DOM node to stick in right rail (i.e. your sticky chart).offset
: Object to manually set the offset for both directions. Must include both an up and down key and both values should be strings (i.e.{up:"50%",down:"0"}
)
Whenever a narrative
function or onChange
is called, it’s passed an event object as an argument.
var ts = new TwoStep({
elements: document.querySelectorAll('.item'),
onChange: function(event) {
console.log(event);
}
});
Here’s what you can find in a typical event object:
{
index: 0,
direction: 'up', // or 'down', or null
element: < element > // DOM node corresponding to index
}
Check event.direction
:
var ts = new TwoStep({
elements: document.querySelectorAll('.item'),
onChange: function(event) {
if (event.direction === 'up') {
// do something
} else if (event.direction === 'down') {
// do something else
}
}
});
.goTo(index, scroll)
: Activate item atindex
. Ifscroll
is true, will animate to position. Returns a promise which resolves when scrolling is complete..disable()
: Prevent waypoints from firing and unstick stuck element, if present.enable()
: Return to standard behaviour
Make sure you have Node.js installed. Then run:
npm install
npm run start
To run tests, open tests.html
in your browser and wait a couple of seconds.
- Graph Scroll
- Scrollama
- The Pudding’s article How to implement scrollytelling with six different libraries
For something a bit different, see scrollWatcher, also made by WSJ.
v1.0.0 (November 27, 2017)
- Initial public release