-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroller.js
71 lines (54 loc) · 1.77 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
65
66
67
68
69
70
function scroller(){
let container = d3.select('body')
// let container = d3.select('.scrollersections')
let dispatch = d3.dispatch('active', 'progress');
let sections = d3.selectAll('.step')
let sectionPositions
let currentIndex = -1
let containerStart = 0;
function scroll(){
d3.select(window)
.on('scroll.scroller', position)
.on('resize.scroller', resize)
resize();
let timer = d3.timer(function() {
position();
timer.stop();
});
}
function resize(){
sectionPositions = [];
let startPos;
sections.each(function(d, i) {
let top = this.getBoundingClientRect().top;
if (i === 0 ){
startPos = top;
}
sectionPositions.push(top - startPos)
});
}
function position() {
let pos = window.pageYOffset -1100 - containerStart;
let sectionIndex = d3.bisect(sectionPositions, pos);
sectionIndex = Math.min(sections.size()-1, sectionIndex);
if (currentIndex !== sectionIndex){
dispatch.call('active', this, sectionIndex);
currentIndex = sectionIndex;
}
let prevIndex = Math.max(sectionIndex - 1, 0);
let prevTop = sectionPositions[prevIndex]
let progress = (pos - prevTop) / (sectionPositions[sectionIndex] - prevTop);
dispatch.call('progress', this, currentIndex, progress)
}
scroll.container = function(value) {
if (arguments.legth === 0){
return container
}
container = value
return scroll
}
scroll.on = function(action, callback){
dispatch.on(action, callback)
};
return scroll;
}