-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.js
219 lines (168 loc) · 6.89 KB
/
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
function Scroll(containerEl, sectionEls /* Object with integer parameters and element values*/, graphEl, transitions /*Object with integer params and transition vals*/) {
this.container = containerEl;
this.sections = sectionEls;
this.graph = graphEl;
this.transitions = transitions;
var self = this;
launchScroll(self);
}
function launchScroll(self) {
var sections = self.sections;
var graph = self.graph;
var sectionsPositions = {};
var thresholds;
var orientation;
var dimensions;
var graphWidth;
var graphHeight;
var headerBox;
var headerHeightPx;
var containerRight;
var containerLeft;
var bodyMarginPx;
var containerBorderWidthPx;
var offsetIntervals =[];
var activeIndex;
var progress;
var i;
for (i in sections) {
var ref = sections[i];
sections[i] = {
el: ref,
passed: false,
active: false
};
sectionsPositions[i] = {
top: null,
bottom: null
};
}
handleResize();
window.addEventListener("resize", handleResize);
window.addEventListener("scroll", handleScroll);
function handleScroll() {
var i;
var el;
var bR;
var section;
var activeIndex_;
for (i = 0; i<offsetIntervals.length; i++){
if(pageYOffset<offsetIntervals[0][0]){
activeIndex_=-Infinity;
break;
} else if(pageYOffset>=offsetIntervals[offsetIntervals.length-1][1]){
activeIndex_=Infinity;
break;
} else if (pageYOffset>=offsetIntervals[i][0]&&pageYOffset<offsetIntervals[i][1]){
activeIndex_ = i;
break;
} else if (i<offsetIntervals.length-1&&pageYOffset>=offsetIntervals[i][1]&&pageYOffset<offsetIntervals[i+1][0]){
activeIndex_ = -1;
break;
}
}
if (activeIndex!==activeIndex_){
if(activeIndex_===-Infinity&&(activeIndex>-Infinity||activeIndex===undefined)){
d3.select(graph)
.style("position", "absolute")
.style("top", sections[0].el.getBoundingClientRect().top- d3.select(container).node().getBoundingClientRect().top - thresholds[1] + parseInt(headerHeightPx)+parseInt(bodyMarginPx)+"px")
.style("bottom", null)
.style("right",0)
.style("left",null);
}
if((activeIndex===-Infinity||activeIndex===Infinity)&&(activeIndex_>-Infinity&&activeIndex_<Infinity)){
d3.select(graph).style("position", "fixed")
.style("top", parseInt(headerHeightPx)+parseInt(bodyMarginPx)+"px")
.style("right", document.getElementsByTagName("body")[0].getBoundingClientRect().right+ parseInt(bodyMarginPx)- containerRight+parseInt(containerBorderWidthPx)+"px");
}
if(activeIndex_===Infinity&&activeIndex<Infinity){
d3.select(graph)
.style("position", "absolute")
.style("top", null)
.style("bottom", d3.select(container).node().getBoundingClientRect().bottom - sections[sections.length-1].el.getBoundingClientRect().top+ thresholds[0]- parseInt(bodyMarginPx)- parseInt(headerHeightPx)- graphHeight+ "px")
.style("right",0+"px")
.style("left",null);
}
if(activeIndex_>-Infinity&&activeIndex_<Infinity){
for(i=0; i<sections.length;i++){
sections[i].el.className = "section";
}
if(activeIndex_>-1){
sections[activeIndex_].el.className = "section active";
self.transitions[activeIndex_]?self.transitions[activeIndex_]():0;
}
}
activeIndex=activeIndex_;
}
if(activeIndex===-Infinity&&progress!==0){
progress =0;
} else if(activeIndex===-1&&progress!==1){
progress =1;
} else if (activeIndex>-1&&activeIndex<Infinity){
progress = (pageYOffset - offsetIntervals[activeIndex][0])/(offsetIntervals[activeIndex][1] - offsetIntervals[activeIndex][0]);
} else if (activeIndex===Infinity&&progress!==1){
progress =1;
}
}
function getThresholds(){
d3.select("#thresholdTop").remove();
d3.select("#thresholdBottom").remove();
if (orientation==="horizontal"){
thresholds = {
0: 0.1*(dimensions[1] - parseInt(headerHeightPx)- 2*parseInt(bodyMarginPx))+parseInt(headerHeightPx)+parseInt(bodyMarginPx),
1: 0.9*(dimensions[1] - parseInt(headerHeightPx)- 2*parseInt(bodyMarginPx))+parseInt(headerHeightPx)+parseInt(bodyMarginPx)
}
d3.select("body").append("div").attr("id","thresholdTop").style("top",thresholds[0]+"px");
d3.select("body").append("div").attr("id","thresholdBottom").style("top",thresholds[1]+"px");
} else if(orientation==="vertical"){
thresholds = {
0: 0.1*(dimensions[1]/2 - 2*parseInt(bodyMarginPx))+parseInt(bodyMarginPx)+dimensions[1]/2,
1: 0.9*(dimensions[1]/2 - 2*parseInt(bodyMarginPx))+parseInt(bodyMarginPx)+dimensions[1]/2
}
d3.select("body").append("div").attr("id","thresholdTop").style("top",thresholds[0]+"px");
d3.select("body").append("div").attr("id","thresholdBottom").style("top",thresholds[1]+"px");
}
}
function handleResize() {
headerBox = document.getElementById("header").getBoundingClientRect();
headerHeightPx = d3.select("#header").style("height");
containerRight = d3.select(container).node().getBoundingClientRect().right;
containerLeft = d3.select(container).node().getBoundingClientRect().left;
graphWidth = d3.select(graph).node().getBoundingClientRect().right - d3.select(graph).node().getBoundingClientRect().left;
bodyMarginPx = d3.select("body").style("margin-right");
containerBorderWidthPx = d3.select(container).style("border-left-width");
graphBorderWidthPx = d3.select(graph).style("border-left-width");
d3.select("body").append("div")
.attr("id","dimensions")
.style("position", "fixed")
.style("width","100%")
.style("height","100%");
dimensions = [containerRight -containerLeft- 2*parseInt(containerBorderWidthPx), d3.select("#dimensions").node().getBoundingClientRect().bottom - d3.select("#dimensions").node().getBoundingClientRect().top];
d3.select("#dimensions").remove();
orientation = dimensions[0]>=dimensions[1]?"horizontal":"vertical";
if(orientation==="horizontal") {
graphWidth = dimensions[0]/2;
graphHeight = dimensions[1] - 2*parseInt(bodyMarginPx)- parseInt(headerHeightPx);
} else if(orientation==="vertical"){
graphWidth = dimensions[0];
graphHeight = dimensions[1]/2 - 2*parseInt(bodyMarginPx)- parseInt(headerHeightPx);
}
d3.select(graph)
.style("width", graphWidth+"px")
.style("height", graphHeight+"px");
var i;
for (i in sections){
sections[i].active=false;
sections[i].passed=false;
}
if(self.transitions.resize) self.transitions.resize(graphWidth - 2*parseInt(graphBorderWidthPx), graphHeight- 2*parseInt(graphBorderWidthPx));
getThresholds();
for (i=0; i<sections.length; i++) {
offsetIntervals[i] = [
i===0?sections[i].el.getBoundingClientRect().top + pageYOffset - thresholds[1]:Math.max(sections[i].el.getBoundingClientRect().top + pageYOffset - thresholds[1], offsetIntervals[i-1][1]),
sections[i].el.getBoundingClientRect().top + pageYOffset - thresholds[0]];
}
console.log(offsetIntervals);
handleScroll();
}
}