forked from 1wheel/graph-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph-scroll.js
136 lines (111 loc) · 3.07 KB
/
graph-scroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function graphScroll() {
var windowHeight,
dispatch = d3.dispatch("scroll", "active"),
sections = d3.select('null'),
i = NaN,
sectionPos = [],
n,
graph = d3.select('null'),
isFixed = null,
isBelow = null,
container = d3.select('body'),
containerStart = 0,
belowStart,
eventId = Math.random()
function reposition(){
var i1 = 0
sectionPos.forEach(function(d, i){
if (d < pageYOffset - containerStart + 200) i1 = i
})
i1 = Math.min(n - 1, i1)
if (i != i1){
sections.classed('graph-scroll-active', function(d, i){ return i === i1 })
dispatch.active(i1)
i = i1
}
var isBelow1 = pageYOffset > belowStart
if (isBelow != isBelow1){
isBelow = isBelow1
graph.classed('graph-scroll-below', isBelow)
}
var isFixed1 = !isBelow && pageYOffset > containerStart
if (isFixed != isFixed1){
isFixed = isFixed1
graph.classed('graph-scroll-fixed', isFixed)
}
}
function resize(){
sectionPos = []
var startPos
sections.each(function(d, i){
if (!i) startPos = this.getBoundingClientRect().top
sectionPos.push(this.getBoundingClientRect().top - startPos) })
var containerBB = container.node().getBoundingClientRect()
var graphBB = graph.node().getBoundingClientRect()
containerStart = containerBB.top + pageYOffset
belowStart = containerBB.bottom - graphBB.height + pageYOffset
}
function keydown() {
if (!isFixed) return
var delta
switch (d3.event.keyCode) {
case 39: // right arrow
if (d3.event.metaKey) return
case 40: // down arrow
case 34: // page down
delta = d3.event.metaKey ? Infinity : 1 ;break
case 37: // left arrow
if (d3.event.metaKey) return
case 38: // up arrow
case 33: // page up
delta = d3.event.metaKey ? -Infinity : -1 ;break
case 32: // space
delta = d3.event.shiftKey ? -1 : 1
;break
default: return
}
var i1 = Math.max(0, Math.min(i + delta, n - 1))
d3.select(document.documentElement)
.interrupt()
.transition()
.duration(500)
.tween("scroll", function() {
var i = d3.interpolateNumber(pageYOffset, sectionPos[i1] + containerStart)
return function(t) { scrollTo(0, i(t)) }
})
d3.event.preventDefault()
}
var rv ={}
rv.container = function(_x){
if (!_x) return container
container = _x
return rv
}
rv.graph = function(_x){
if (!_x) return graph
graph = _x
return rv
}
rv.eventId = function(_x){
if (!_x) return eventId
eventId = _x
return rv
}
rv.sections = function (_x){
if (!_x) return sections
sections = _x
n = sections.size()
d3.select(window)
.on('scroll.gscroll' + eventId, reposition)
.on('resize.gscroll' + eventId, resize)
.on('keydown.gscroll' + eventId, keydown)
resize()
d3.timer(function() {
reposition()
return true
})
return rv
}
d3.rebind(rv, dispatch, "on")
return rv
}