-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
99 lines (88 loc) · 2.4 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Optimized List</title>
<!--<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">-->
<!--<script src="js/jquery-1.10.2.js"></script>-->
<script src="js/react-0.12.0.js"></script>
<script src="js/JSXTransformer-0.12.0.js"></script>
<!--<script src="js/lodash.underscore.js"></script>-->
<!--<script src="js/accounting.js"></script>-->
<!--<script src="js/faker.js"></script>-->
</head>
<style>
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body, html {
margin: 0;
padding: 0;
background-color: white;
}
ul {
list-style: none;
margin: 0;
padding: 0;
position: relative;
top: 0;
}
ul li {
background-color: #f0f0f0;
margin-bottom: 1px;
padding-top: 7px;
padding-left: 20px;
height: 30px;
}
</style>
<body style="height: 100%">
<script type="text/jsx">
/*** @jsx React.DOM */
var App = React.createClass({
getInitialState: function() {
return { pageY: 0,
pageHeight: window.innerHeight
};
},
componentDidMount: function() {
var that = this;
var numItems = this.props.items.length;
document.querySelector('.list').style.height = numItems * 31 + 'px';
var ul = document.querySelector('ul');
ul.style.top = this.state.pageY + 'px';
window.addEventListener('scroll', function(e) {
that.setState({ pageY: Math.max(e.pageY || window.pageYOffset, 0) });
that.setState({ pageHeight: window.innerHeight });
});
},
render: function() {
var pageY = this.state.pageY;
var begin = pageY / 31 | 0;
// Add 2 so that the top and bottom of the page are filled with
// next/prev item, not just whitespace if item not in full view
var end = begin + (this.state.pageHeight / 31 | 0 + 2);
var offset = pageY % 31;
var listItems = this.props.items.slice(begin, end).map(function(item) {
return <li>{item.title}</li>
});
return (
<div className='list' style={ { 'position': 'relative', 'top': (-offset) + 'px' } }>
<ul style={ {'top': this.state.pageY + 'px' } }>
{listItems}
</ul>
</div>
);
}
});
var items = [];
for(var i=0; i<5000; i++) {
items.push({
title: 'Foo Bar ' + i
});
}
React.renderComponent(<App items={items} />, document.body);
</script>
</body>
</html>