GithubHelp home page GithubHelp logo

pombredanne / scroll-watcher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wsj/scroll-watcher

0.0 1.0 0.0 9 KB

A small JavaScript library for scroll-based data-driven graphics.

License: ISC License

HTML 69.79% JavaScript 30.21%

scroll-watcher's Introduction

scrollWatcher ๐Ÿ“œ๐Ÿ‘€

A small JavaScript library for scroll-based data-driven graphics (without any nasty scrolljacking). scrollWatcher sticks an element at a fixed position onscreen, and then provides the distance scrolled through its (much taller) parent as a percentage.

As seen on WSJ.com in How Fed Rates Move Markets and What ECB Stimulus Has Done.

Is scrollWatcher the right library for you?

  • Want something to always stick on the page? Use CSS position: fixed;.
  • Want something to stick when you scroll past it? Use jQuery fixto plugin or jQuery Sticky.
  • Want a sticky header that hides on scroll? Use headroom.js.
  • Want to trigger events on scroll? Use Waypoints.
  • If you want to use scroll as a way of interacting with a data-driven graphic without scrolljacking, use scrollWatcher.

Quickstart

  1. Include jQuery and the sticky-positioning fixto plugin on the page.

  2. In your HTML, you'll need a tall outer element, with one much shorter element inside of it.

    <div class="outer" style="height: 2000px;"></div>
        <div class="inner"></div>
    </div>
  3. In your JavaScript, you'll need to call scrollWatcher with a configuration object using parent and onUpdate arguments. The function passed into onUpdate will run every 20 miliseconds.

    scrollWatcher({
        parent: '.outer',
        onUpdate: function( scrollPercent, parentElement ){
            $('.inner').text('Scrolled '+scrollPercent+'% through the parent.');
        }
    });

Examples

Check out the source code to see how these are used.

Other features

Starting and stopping

If you create a new instance of scrollWatcher:

var myWatcher = scrollWatcher({
    onUpdate: function( scrollPercent, parentElement ){
        console.log('Scrolled '+scrollPercent+'% through the parent.');
    },
    parent: '.outer'
});

... you can then start, pause and stop at any time.

// stop checking but keep stuck
myWatcher.pause();

// stop checking and unstick
myWatcher.stop();

// start checking and restick
myWatcher.start();

Check if active

There are two read-only properties:

  • active is true when the scrollWatcher instance is currently onscreen (and running).

    var isActive = myWatcher.active;
  • hasBeenActive is true when the scrollWatcher instance has been onscreen (and run) at least once.

    var isOrWasActive = myWatcher.hasBeenActive;

You may want to use these to (for example) hide a "keep scrolling!" message.

Check for device support

Use scrollWatcher.supported() to check whether or not scrollWatcher will work in the current browser.

Browser support

scrollWatcher works on all modern browsers, including IE 9 and up. However, it does not work on iOS 7 and lower due to the way Safari handles CSS position: fixed;.

Testing

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

Version history

v1.0.0 (April 19, 2016)

  • Initial public release

License

ISC

scroll-watcher's People

Contributors

ejb avatar

Watchers

 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.