-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
205 lines (175 loc) · 7.14 KB
/
index.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
(function(){
const wrapElm = document.querySelector('.wrap')
const dataLen = 100000
const data = []
for(let i = 0; i < dataLen; i++) {
data.push(i)
}
const domHeight = 33
const domCache = []
const domCacheLength = 200
const BoundaryCount = 50
const totalHeight = domCacheLength * domHeight
const listWraper = document.createElement('div')
listWraper.className = 'list-wraper'
const scrollBar = document.createElement('div')
scrollBar.className = 'scroll-bar'
for (let i = 0; i < domCacheLength; i++) {
const div = document.createElement('div')
div.innerText = data[i]
div.className = 'item'
domCache.push(div)
listWraper.appendChild(div)
}
wrapElm.appendChild(listWraper)
wrapElm.appendChild(scrollBar)
const listWraperHeight = listWraper.offsetHeight
const visibleItemCount = Math.ceil(listWraperHeight/domHeight)
const scrollBarHeight = Math.max(60, (listWraperHeight / totalHeight) * listWraperHeight)
scrollBar.style.height = scrollBarHeight + 'px'
const maxMoveCount = Math.floor((domCacheLength - visibleItemCount) / 2 - BoundaryCount)
let currentIndex = 0
let cacheIndex = 0
let lastScrollTop = 0, currentScrollTop = 0
let selectDom
let selectIndex
function resetSelectDom() {
if(selectDom) {
selectDom.className = 'item'
}
const sIndex = +domCache[0].innerText
if (selectIndex >= sIndex && selectIndex <= sIndex + domCacheLength - 1) {
selectDom = domCache[selectIndex - sIndex]
selectDom.className = 'item selected'
}
}
function putTopDomToBottom() {
// console.log('------------------------------------putTopDomToBottom 321---------------------------------')
// console.log('currentScrollTop', currentScrollTop)
// console.log('currentIndex', currentIndex)
// console.log('cacheIndex', cacheIndex)
const fragment = document.createDocumentFragment()
const moveCount = Math.min(dataLen - (currentIndex + domCacheLength - 1 - cacheIndex) - 1, maxMoveCount)
for (let i = 0; i < moveCount; i++) {
const dom = domCache.shift()
dom.innerText = data[currentIndex + (domCacheLength - cacheIndex - 1) + 1 + i]
fragment.appendChild(dom)
domCache.push(dom)
}
resetSelectDom()
listWraper.appendChild(fragment)
// console.log(domCache.map(dom => dom.innerText))
}
function putBottomDomToTop() {
// console.log('------------------------------------putBottomDomToTop 123---------------------------------')
// console.log('currentScrollTop', currentScrollTop)
// console.log('currentIndex', currentIndex)
// console.log('cacheIndex', cacheIndex)
const fragment = document.createDocumentFragment()
const moveCount = Math.min(currentIndex - cacheIndex, maxMoveCount)
for (let i = 0; i < moveCount; i++) {
const dom = domCache.pop()
dom.innerText = data[currentIndex - cacheIndex - 1 - i]
fragment.prepend(dom)
domCache.unshift(dom)
}
resetSelectDom()
listWraper.prepend(fragment)
// console.log(domCache.map(dom => dom.innerText))
}
const upperSide = BoundaryCount * domHeight
const downSide = (domCacheLength - BoundaryCount - visibleItemCount) * domHeight
let isScroll = false, startY
const maxTop = listWraperHeight - scrollBarHeight
const maxIndex = dataLen - Math.ceil(listWraperHeight/domHeight)
const maxCount = domCacheLength - Math.ceil(listWraperHeight/domHeight)
const maxScrollHeight = listWraper.scrollHeight
function listScroll() {
console.log('scroll')
currentScrollTop = listWraper.scrollTop
cacheIndex = Math.floor(currentScrollTop / domHeight)
currentIndex = +domCache[cacheIndex].innerText
scrollBar.style.top = Math.min(currentIndex / (dataLen - visibleItemCount), 1) * maxTop + 'px'
if (currentScrollTop > lastScrollTop) {
// 向下滚动时,如果底部只剩下50个元素,并且底部还剩下超过50条数据,就从顶部挪过来一部分元素,保持页面的上下对称
if (currentScrollTop >= downSide && currentIndex < dataLen - 1) {
putTopDomToBottom()
}
} else if (currentScrollTop < lastScrollTop) {
// 向上滚动时,如果顶部只剩下50个元素,并且顶部还剩下超过50条数据,就从底部挪过来一部分元素,保持页面的上下对称
if (currentScrollTop <= upperSide && currentIndex > 0) {
putBottomDomToTop()
}
}
lastScrollTop = currentScrollTop
document.onkeydown = keydownFun
}
// 当用鼠标滚动
listWraper.addEventListener('scroll', listScroll)
scrollBar.addEventListener('mousedown', function(e) {
isScroll = true
startY = e.pageY
listWraper.removeEventListener('scroll', listScroll)
})
document.body.addEventListener('mousemove', function(e) {
if (!isScroll) return
const offsetY = e.pageY - startY
const curTop = parseFloat(scrollBar.style.top) || 0
const toTop = Math.max(0, Math.min(maxTop, curTop + offsetY))
scrollBar.style.top = toTop + 'px'
const startIndex = toTop === 0 ? 0 : toTop === maxTop ? maxIndex : Math.floor((toTop / maxTop) * maxIndex) // 当前可视区域顶端索引
const topCount = toTop === 0 ? 0 : toTop === maxTop ? maxCount : Math.floor((toTop / maxTop) * maxCount)// 可视区域顶部以上元素条数
const scrollPos = toTop === 0 ? 0 : toTop === maxTop ? maxScrollHeight : Math.floor((toTop / maxTop) * maxScrollHeight)// wrapper滚动位置
for (let i = 0; i < domCacheLength; i++) {
const dom = domCache[i]
dom.innerText = data[startIndex - topCount + i]
}
startY = e.pageY
currentIndex = startIndex
listWraper.scrollTo({
top: scrollPos
})
resetSelectDom()
})
document.body.addEventListener('mouseup', function(e) {
isScroll = false
listWraper.addEventListener('scroll', listScroll)
})
document.body.addEventListener('click', function(e) {
if(e.target.className.includes('item')) {
if (selectDom) {
selectDom.className = 'item'
}
selectDom = e.target
selectIndex = +selectDom.innerText
selectDom.className = 'item selected'
}
})
function keydownFun(e) {
e.preventDefault()
if (e.keyCode === 38) {
const preVisible = selectIndex >= currentIndex && selectIndex <= currentIndex + visibleItemCount - 1
selectIndex -= 1
selectIndex = Math.min(dataLen - 1, Math.max(0, selectIndex))
if (selectIndex < currentIndex && preVisible) {
document.onkeydown = function(){}
listWraper.scrollTo({
top: listWraper.scrollTop - (currentIndex - selectIndex) * domHeight
})
}
resetSelectDom()
} else if (e.keyCode === 40) {
const preVisible = selectIndex >= currentIndex && selectIndex <= currentIndex + visibleItemCount - 1
selectIndex += 1
selectIndex = Math.min(dataLen - 1, Math.max(0, selectIndex))
if (selectIndex > currentIndex + visibleItemCount - 1 && preVisible) {
document.onkeydown = function(){}
listWraper.scrollTo({
top: listWraper.scrollTop + (selectIndex - (currentIndex + visibleItemCount - 1)) * domHeight
})
}
resetSelectDom()
}
}
document.onkeydown = keydownFun
})()