├── LICENSE
├── Readme.md
├── examples
├── active.html
├── d3-scale-time.html
├── d3-scale.html
└── tall-child.html
├── index.html
├── scroll-watcher.js
└── tests.html
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2016 Dow Jones & Company
2 |
3 | Permission to use, copy, modify, and/or distribute this software for any
4 | purpose with or without fee is hereby granted, provided that the above
5 | copyright notice and this permission notice appear in all copies.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | # scrollWatcher 📜👀
2 |
3 | A small JavaScript library for scroll-based data-driven graphics (without any nasty [scrolljacking](http://blog.arronhunt.com/post/66973746030/stop-scrolljacking)). scrollWatcher sticks an element at a fixed position onscreen, and then provides the distance scrolled through its (much taller) parent as a percentage.
4 |
5 | - [Demo here](http://wsj.github.io/scroll-watcher/)
6 |
7 | As seen on WSJ.com in [How Fed Rates Move Markets](http://graphics.wsj.com/reacting-to-fed-rates/) and [What ECB Stimulus Has Done](http://graphics.wsj.com/what-ecb-qe-stimulus-has-done/).
8 |
9 | ## Is scrollWatcher the right library for you?
10 |
11 | - Want something to always stick on the page? Use [CSS `position: fixed;`](https://css-tricks.com/almanac/properties/p/position/#article-header-id-2).
12 | - Want something to stick when you scroll past it? Use [jQuery fixto plugin](https://github.com/bbarakaci/fixto) or [jQuery Sticky](https://github.com/garand/sticky).
13 | - Want a sticky header that hides on scroll? Use [headroom.js](http://wicky.nillia.ms/headroom.js/).
14 | - Want to trigger events on scroll? Use [Waypoints](http://imakewebthings.com/waypoints/).
15 | - **If you want to use scroll as a way of interacting with a data-driven graphic without scrolljacking, use scrollWatcher.**
16 |
17 | ## Quickstart
18 |
19 | 1. Include [jQuery](http://jquery.com) and the sticky-positioning [fixto plugin](https://github.com/bbarakaci/fixto) on the page.
20 |
21 | 2. In your HTML, you'll need a tall outer element, with one much shorter element inside of it.
22 |
23 | ```html
24 |
25 |
26 |
27 | ```
28 |
29 | 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.
30 |
31 | ```js
32 | scrollWatcher({
33 | parent: '.outer',
34 | onUpdate: function( scrollPercent, parentElement ){
35 | $('.inner').text('Scrolled '+scrollPercent+'% through the parent.');
36 | }
37 | });
38 | ```
39 |
40 | ## Examples
41 |
42 | Check out the source code to see how these are used.
43 |
44 | - [Basic demo](http://wsj.github.io/scroll-watcher/)
45 | - [Using a D3 scale](http://wsj.github.io/scroll-watcher/examples/d3-scale.html)
46 | - [Using a D3 time scale](http://wsj.github.io/scroll-watcher/examples/d3-scale-time.html)
47 | - [Checking if the user has got there yet](http://wsj.github.io/scroll-watcher/examples/active.html)
48 |
49 | ## Other features
50 |
51 | ### Starting and stopping
52 |
53 | If you create a new instance of scrollWatcher:
54 |
55 | ```js
56 | var myWatcher = scrollWatcher({
57 | onUpdate: function( scrollPercent, parentElement ){
58 | console.log('Scrolled '+scrollPercent+'% through the parent.');
59 | },
60 | parent: '.outer'
61 | });
62 | ```
63 |
64 | ... you can then start, pause and stop at any time.
65 |
66 | ```js
67 | // stop checking but keep stuck
68 | myWatcher.pause();
69 |
70 | // stop checking and unstick
71 | myWatcher.stop();
72 |
73 | // start checking and restick
74 | myWatcher.start();
75 | ```
76 |
77 | ### Check if active
78 |
79 | There are two read-only properties:
80 |
81 | - `active` is true when the scrollWatcher instance is currently onscreen (and running).
82 |
83 | ```js
84 | var isActive = myWatcher.active;
85 | ```
86 |
87 | - `hasBeenActive` is true when the scrollWatcher instance has been onscreen (and run) at least once.
88 |
89 | ```js
90 | var isOrWasActive = myWatcher.hasBeenActive;
91 | ```
92 |
93 | You may want to use these to (for example) hide a "keep scrolling!" message.
94 |
95 | ### Check for device support
96 |
97 | Use `scrollWatcher.supported()` to check whether or not scrollWatcher will work in the current browser.
98 |
99 | ## Browser support
100 |
101 | 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;`.
102 |
103 | ## Testing
104 |
105 | To run tests, open [`tests.html`](http://github.com/wsj/scroll-watcher/tests.html) in your browser and wait a couple of seconds.
106 |
107 | ## Version history
108 |
109 | **v1.0.0** (April 19, 2016)
110 |
111 | - Initial public release
112 |
113 | ## License
114 |
115 | [ISC](/LICENSE)
116 |
--------------------------------------------------------------------------------
/examples/active.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
58 |
59 |
In this example, the child element is taller than the screen height, but still sticks in the container. It's a rare case, but can occur (for example) on smartphones.
A JavaScript library for navigating data using scroll, by The Wall Street Journal.
77 |
78 |
79 |
80 |
81 |
scrollWatcher is a small library for scroll-based data-driven graphics (without any nasty scrolljacking). It sticks an element at a fixed position onscreen, and then provides the distance scrolled through its (much taller) parent as a percentage.