-
Notifications
You must be signed in to change notification settings - Fork 24
/
scroller.js
65 lines (52 loc) · 1.67 KB
/
scroller.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
export function scroller(visFuncList){
const sections = d3.selectAll('.step');
const containerStart = 0;
let currentIndex = -1
let sectionPositions = [];
function execute() {
// 3. 아래 내용 따라 구현하기 start
d3.select(window)
.on('scroll.scroller', showCurrentVis)
.on('resize.scroller', setSectionPositions)
// end
setSectionPositions();
const timer = d3.timer(function() {
showCurrentVis();
timer.stop();
});
}
function setSectionPositions() {
let startPos;
sectionPositions = [];
// 1. 아래 내용 따라 구현하기 start
d3.selectAll('.step').each(function(d, i) {
const top = this.getBoundingClientRect().top;
if (i === 0) {
startPos = top;
}
sectionPositions.push(top - startPos)
});
// end
}
function showCurrentVis() {
const pos = window.pageYOffset - 300 - containerStart;
// 2. 아래 내용 따라 구현하기 start
let sectionIndex = d3.bisect(sectionPositions, pos);
sectionIndex = Math.min(d3.selectAll(".step").size()-1, sectionIndex);
// end
if (currentIndex !== sectionIndex){
activate(sectionIndex);
currentIndex = sectionIndex;
}
}
function activate(index) {
d3.selectAll(".step")
.transition()
.duration(500)
.style("opacity", function (d, i) {
return i === index ? 1 : 0.1;
});
visFuncList[index]();
}
return execute;
}