Code Monkey home page Code Monkey logo

two-step's Introduction

TwoStep

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:

Demos

TwoStep is highly flexible, and can be used in range of designs.

These demos are also meant as starting points for new projects.

Setup

  1. Include jQuery, Waypoints, FixTo and TwoStep on the page.

  2. Set up HTML and CSS as best fits your project. See source code from the demos above for inspiration.

  3. In your JavaScript:

var ts = new TwoStep({
    elements: document.querySelectorAll('.slide-item'),
    onChange: function(event) {
        console.log(event.index);
    }
});

Options

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"})

Event object

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
}

How to get scroll direction

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
        }
    }
});

Public methods

  • .goTo(index, scroll): Activate item at index. If scroll 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

Compiling from scratch

Make sure you have Node.js installed. Then run:

npm install
npm run start

Running tests

To run tests, open tests.html in your browser and wait a couple of seconds.

Alternatives to TwoStep

For something a bit different, see scrollWatcher, also made by WSJ.

Version history

v1.0.0 (November 27, 2017)

  • Initial public release

License

ISC

two-step's People

Contributors

ejb avatar

Watchers

Daniele Palumbo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.