-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollamaCode.js
More file actions
151 lines (128 loc) · 4.67 KB
/
Copy pathscrollamaCode.js
File metadata and controls
151 lines (128 loc) · 4.67 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var main = d3.select("main");
var scrolly = main.select("#scrolly");
var figure = scrolly.select("figure");
var article = scrolly.select("article");
var step = article.selectAll(".step");
var scroller = scrollama();
function handleResize() {
var stepH = Math.floor(window.innerHeight * 0.75);
step.style("height", stepH + "px");
step.style("width", "250px")
var figureHeight = window.innerHeight / 2;
var figureMarginTop = (window.innerHeight - figureHeight) / 2;
figure
.style("height", figureHeight + "px")
.style("top", figureMarginTop + "px");
scroller.resize();
}
// event handler
let chartState = {
baseChartDrawn: false,
greenLinesDrawn: false,
};
function handleStepEnter(response) {
console.log(response); // response = { element, direction, index }
let currentIndex = response.index;
let currentDirection = response.direction;
if (currentIndex !== 3 && currentIndex !== 4) {
TooltipManager.destroy();
}
// add color to current step only
step.classed("is-active", function(d, i) {
return i === currentIndex;
});
// update graphic based on step
switch(currentIndex){
case 0:
if (!chartState.baseChartDrawn) {
drawLines();
chartState.baseChartDrawn = true;
}
if (currentDirection === "up") {
toggleElementOpacity(greenRect, 500, 0);
}
break;
case 1: //step 2
chartState.greenLinesDrawn = false;
if (currentDirection === "down") {
showRect();
toggleElementOpacity(greenRect, 500, 0.5);
}
if(currentDirection === "up") {
svgCountry.selectAll(".green-line").remove();
svgCountry.selectAll(".newYaxis").remove();
if (!chartState.baseChartDrawn) {
drawLines();
showRect();
chartState.baseChartDrawn = true;
}
}
break;
case 2:
chartState.baseChartDrawn = false;
if (!chartState.greenLinesDrawn){
greenLines();
chartState.greenLinesDrawn = true;
}
if(currentDirection === "up") {
toggleElementOpacity(greenRect, 500, 0.5);
toggleElementOpacity(svgCountry.select(".axis"), 500, 1);
toggleElementOpacity(svgCountry.select(".newYaxis"), 500, 1);
toggleElementOpacity(svgMap, 500, 0);
toggleElementOpacity(labelHover, 500, 0);
}
break;
case 3:
TooltipManager.destroy();
toggleElementOpacity(svgCountry.select(".axis"), 1000, 0);
toggleElementOpacity(svgCountry.select(".newYaxis"), 1000, 0);
toggleElementOpacity(greenRect, 1000, 0);
chartState.greenLinesDrawn = false;
showMap();
toggleElementOpacity(labelHover, 500, 1);
if (currentDirection === "down") {
toggleElementOpacity(svgMap, 500, 1);
}
if(currentDirection === "up") {
toggleElementOpacity(svgMap, 500, 1);
toggleElementOpacity(incomeBar, 1000, 0);
toggleElementOpacity(regionBar, 1000, 0);
}
break;
case 4:
toggleElementOpacity(svgCountry.select(".axis"), 500, 0);
toggleElementOpacity(svgCountry.select(".newYaxis"), 500, 0);
toggleElementOpacity(svgCountry.select(".line"), 500, 0);
toggleElementOpacity(greenRect, 500, 0);
toggleElementOpacity(svgMap, 500, 0);
createBars();
toggleElementOpacity(incomeBar, 500, 1);
toggleElementOpacity(regionBar, 500, 1);
toggleElementOpacity(labelHover, 500, 1);
front(incomeBar);
front(regionBar);
front(TooltipManager.currentBarTooltip);
break;
default:
TooltipManager.destroy();
break;
}
}
function setupStickyfill() {
d3.selectAll(".sticky").each(function() {
Stickyfill.add(this);
});
}
function init() {
setupStickyfill();
handleResize();
scroller
.setup({
step: "#scrolly article .step",
offset: 0.5,
debug: false
})
.onStepEnter(handleStepEnter);
window.addEventListener("resize", handleResize);
}
init();