• Stars
    star
    509
  • Rank 86,772 (Top 2 %)
  • Language
    HTML
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

scrollers > steppers

graph-scroll.js

Simple scrolling events for d3 graphs. Based on stack

Demo/Documentation

graph-scroll takes a selection of explanatory text sections and dispatches active events as different sections are scrolled into to view. These active events can be used to update a chart's state.

d3.graphScroll()
    .sections(d3.selectAll('#sections > div'))
    .on('active', function(i){ console.log(i + 'th section active') })

The top most element scrolled fully into view is classed graph-scroll-active. This makes it easy to highlight the active section with css:

#sections > div{
	opacity: .3
} 

#sections div.graph-scroll-active{
	opacity: 1;
}

To support headers and intro images/text, we use a container element containing the explanatory text and graph.

<h1>Page Title</div>
<div id='container'>
  <div id='graph'></div>
  <div id='sections'>
    <div>Section 0</div>
    <div>Section 1</div>
    <div>Section 2</div>
  </div>
</div>
<h1>Footer</h1>

If these elements are passed to graphScroll as selections with container and graph, every element in the graph selection will be classed graph-scroll-graph if the top of the container is out of view.

d3.graphScroll()
	.graph(d3.selectAll('#graph'))
	.container(d3.select('#container'))
  .sections(d3.selectAll('#sections > div'))
  .on('active', function(i){ console.log(i + 'th section active') })

When the graph starts to scroll out of view, position: sticky keeps the graph element stuck to the top of the page while the text scrolls by.

#container{
  position: relative;
}

#sections{
  width: 340px;
}

#graph{
  margin-left: 40px;
  width: 500px;
  position: sticky;
  top: 0px;
  float: right;
}

On mobile centering the graph and sections while adding a some padding for the first slide is a good option:

@media (max-width: 925px)  {
  #graph{
    width: 100%;
    margin-left: 0px;
    float: none;
  }

  #sections{
    position: relative;
    margin: 0px auto;
    padding-top: 400px;
  }
}

Adjust the amount of pixels before a new section is triggered is also helpful on mobile (Defaults to 200 pixels):

graphScroll.offset(300)

To update or replace a graphScroll instance, pass a string to eventId to remove the old event listeners:

graphScroll.eventId('uniqueId1')

More Repositories

1

roadtolarissa

JavaScript
182
star
2

d3-starterkit

Snippets and conventions for d3
JavaScript
124
star
3

swoopy-drag

Artisanal label placement for d3 graphics
HTML
120
star
4

hot-server

live-server with hot reloading
JavaScript
99
star
5

gistsnap

snapshot thumbnails for blocks.roadtolarissa.com
JavaScript
34
star
6

reddit-comment-visualizer

downloads and displays user's reddit comments
JavaScript
31
star
7

kindle-tracker

JavaScript
28
star
8

meteors

interactive map of 500 years of witnessed meteor impacts
JavaScript
24
star
9

d3-init

blocks template
CSS
18
star
10

tornado-tuners

interactive map of tornados
JavaScript
18
star
11

d3-jetpack-module

JavaScript
16
star
12

talks

CSS
13
star
13

doc2txt

google doc -> .md
JavaScript
11
star
14

film-strips

JavaScript
8
star
15

unemployment

unemployment rates by gender, race, age, and education
JavaScript
7
star
16

BizarreSierpinskiTriangle

JavaScript
7
star
17

whalewords

word frequency in Moby Dick with d3.js
JavaScript
7
star
18

gistclone

clones a gist or bl.ock.org url
JavaScript
6
star
19

nbadraft

how effective are higher picks in the nba?
JavaScript
5
star
20

connect4-ai

JavaScript
5
star
21

scraping

HTML
4
star
22

scrape-stl

scrape-stl
JavaScript
4
star
23

d3-force-container

constrain particles to an area for forceSimulation in d3v4
JavaScript
4
star
24

deportions

JavaScript
3
star
25

stringline-status

JavaScript
3
star
26

comp-geo

JavaScript
2
star
27

whitehouse-petitions

scrapes and analyses White House petitions
Python
2
star
28

chicago-crime-crossfilter

JavaScript
2
star
29

typing

Lyric Typing Game
JavaScript
2
star
30

learning

HTML
2
star
31

d3-rectangleForce

1
star
32

box-scores

scrapes nba box scores
JavaScript
1
star
33

news-block

filter out sticky banners, pivots to video and newsletter nags
1
star
34

hello-arcadia

Clojure
1
star
35

sound-shapes

JavaScript
1
star
36

nycdatascience

JavaScript
1
star
37

subway-parse

JavaScript
1
star
38

hangout-boardgames

play board games on google hangout
JavaScript
1
star
39

shader-school

JavaScript
1
star
40

webpack-hmr

JavaScript
1
star
41

webgl-workshop

JavaScript
1
star