-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
156 lines (141 loc) · 3.3 KB
/
test.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
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
<!doctype html5>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<title>长列表优化测试</title>
<script src="jquery.js"></script>
<style>
*{
padding: 0px;
margin:0px;
}
li{
height:50px;
line-height:50px;
font-size:20px;
color:red;
background:blue;
border-bottom:solid 1px yellow;
}
</style>
</head>
<body>
<div>长列表测试</div>
<ul>
</ul>
<script>
var ul = $('ul');
var newCounter = 1;
var prefix = $('<li></li>');
var liCache = [];
prefix.css('border','0px');
prefix.css('height','0px');
ul.append(prefix);
ul.append('<li>1</li>');
function checkIsBottom(target){
var winHeight = window.innerHeight;
var scrollY = window.scrollY;
var targetBottom = target.offset().top+target.height();
return targetBottom > scrollY+winHeight;
}
function checkIsTop(target){
var scrollY = window.scrollY;
var targetBottom = target.offset().top+target.height();
return targetBottom > scrollY;
}
function newLi(){
var li;
if( liCache.length == 0 ){
li = $('<li></li>');
console.log('new Li,counter:'+newCounter);
newCounter++;
}else{
li = liCache.pop();
}
return li;
}
function delLi(li){
li.remove();
liCache.push(li);
}
function delTopData(li){
while(true){
//console.log(li.text());
var nextLi = li.next();
var height = li.height();
var prefixHeight = prefix.height();
delLi(li);
prefix.height(prefixHeight+height);
if( checkIsTop(nextLi) ){
break;
}
li = nextLi;
}
}
function addBottomData(){
var lastLi = $('li:last');
var counter = parseInt(lastLi.text());
while(true){
var li = newLi();
li.text(++counter);
ul.append(li);
if(checkIsBottom(li)){
break;
}
}
}
function addTopData(){
var prefixLi = $('li:first');
var firstLi = $('li:first').next();
while(true){
var newFirstLi = newLi();
var prefixHeight = prefix.height();
newFirstLi.text(parseInt(firstLi.text())-1);
firstLi.before(newFirstLi);
prefix.height(prefixHeight-newFirstLi.height());
if( prefixLi.height() == 0 || checkIsTop(prefixLi) == false ){
break;
}
firstLi = newFirstLi;
}
}
function delBottomData(){
var prefixLi = $('li:first');
var li = $('li:last');
var prevLi = li.prev();
while(true){
delLi(li);
li = prevLi;
prevLi = li.prev();
if( prevLi.prev()[0] == prefixLi[0] || checkIsBottom(prevLi) == false){
break;
}
}
}
addBottomData();
$(window).scroll(function(){
//顶部移除节点
var firstLi = $('li:first').next();
if( checkIsTop(firstLi) == false){
delTopData(firstLi);
}
//尾部移除节点
var suffixLi = $('li:last').prev();
if( checkIsBottom(suffixLi)){
delBottomData();
}
//尾部添加节点
var lastLi = $('li:last');
if( checkIsBottom(lastLi) == false ){
addBottomData();
}
//顶部添加节点
var prerfixLi = $('li:first');
if( prerfixLi.height() != 0 && checkIsTop(prerfixLi) == true ){
addTopData();
}
});
</script>
</body>
</html>