forked from d3/d3-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongscroll.js
68 lines (55 loc) · 2.04 KB
/
longscroll.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
(function() {
// Virtual rendering for rows taking up to 1e7px of vertical space.
// By Jason Davies, http://www.jasondavies.com/
d3.longscroll = function() {
var render = null,
size = 0,
position = 0,
rowHeight = 20;
function longscroll(g) {
g.selectAll("div.before").data([0]).enter().append("div").attr("class", "before");
var current = g.selectAll("div.current").data([0]);
current.enter().append("div").attr("class", "current");
g.selectAll("div.after").data([0]).enter().append("div").attr("class", "after");
g.on("scroll.longscroll", function() {
position = Math.floor(this.scrollTop / rowHeight);
scroll(this.scrollTop);
});
scroll(0);
g.each(function() {
var g = d3.select(this);
g.property("scrollTop", +g.select(".before").style("height").replace("px", ""));
});
function scroll(scrollTop) {
g.each(function() {
this.scrollTop = scrollTop;
var g = d3.select(this),
rows = 1 + Math.ceil(this.clientHeight / rowHeight),
position0 = Math.max(0, Math.min(size - rows, position)),
position1 = position0 + rows;
g.select(".before").style("height", position0 * rowHeight + "px");
g.select(".after").style("height", (size - position1) * rowHeight + "px");
var div = g.select(".current").selectAll("div.row")
.data(d3.range(position0, Math.min(position1, size)), String);
div.enter().append("div")
.attr("class", "row");
div.exit().remove();
div.order().call(render);
});
}
}
longscroll.render = function(_) {
return arguments.length ? (render = _, longscroll) : render;
};
longscroll.rowHeight = function(_) {
return arguments.length ? (rowHeight = +_, longscroll) : rowHeight;
};
longscroll.position = function(_) {
return arguments.length ? (position = +_, longscroll) : position;
};
longscroll.size = function(_) {
return arguments.length ? (size = +_, longscroll) : size;
};
return longscroll;
};
})();