Skip to content

Commit 320dc71

Browse files
committed
Support page mode
1 parent c1b925f commit 320dc71

File tree

4 files changed

+72
-31
lines changed

4 files changed

+72
-31
lines changed

README.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
* Big data list with high render performance and efficient.
4040

41-
* You don't have to care about each item size, it will calculate automatic.
41+
* You don't have to care about item size, it will calculate automatic.
4242

4343

4444
## Live demo
@@ -209,6 +209,30 @@ More usages or getting start you can refer to these clearly [examples](https://g
209209
<td></td>
210210
<td>Emited when item resized (mounted), param <code>(id, size)</code>.</td>
211211
</tr>
212+
<tr>
213+
<td><code>direction</code></td>
214+
<td>String</td>
215+
<td>vertical</td>
216+
<td>Scroll direction, available values are <code>vertical</code> and <code>horizontal</code></td>
217+
</tr>
218+
<tr>
219+
<td><code>page-mode</code></td>
220+
<td>Boolean</td>
221+
<td>false</td>
222+
<td>Let virtual list using global document to scroll through the list.</td>
223+
</tr>
224+
<tr>
225+
<td><code>top-threshold</code></td>
226+
<td>Number</td>
227+
<td>0</td>
228+
<td>The threshold to emit <code>totop</code> event, attention to multiple calls.</td>
229+
</tr>
230+
<tr>
231+
<td><code>bottom-threshold</code></td>
232+
<td>Number</td>
233+
<td>0</td>
234+
<td>The threshold to emit <code>tobottom</code> event, attention to multiple calls.</td>
235+
</tr>
212236
<tr>
213237
<td><code>root-tag</code></td>
214238
<td>String</td>
@@ -293,24 +317,6 @@ More usages or getting start you can refer to these clearly [examples](https://g
293317
<td>{}</td>
294318
<td>For using using footer slot, footer slot wrapper element inline style.</td>
295319
</tr>
296-
<tr>
297-
<td><code>direction</code></td>
298-
<td>String</td>
299-
<td>vertical</td>
300-
<td>Scroll direction, available values are <code>vertical</code> and <code>horizontal</code></td>
301-
</tr>
302-
<tr>
303-
<td><code>top-threshold</code></td>
304-
<td>Number</td>
305-
<td>0</td>
306-
<td>The threshold to emit <code>totop</code> event, attention to multiple calls.</td>
307-
</tr>
308-
<tr>
309-
<td><code>bottom-threshold</code></td>
310-
<td>Number</td>
311-
<td>0</td>
312-
<td>The threshold to emit <code>tobottom</code> event, attention to multiple calls.</td>
313-
</tr>
314320
</table>
315321
</details>
316322

src/index.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ const VirtualList = Vue.component('virtual-list', {
6767
} else if (this.offset) {
6868
this.scrollToOffset(this.offset)
6969
}
70+
71+
// in page mode we bind scroll event to document
72+
if (this.pageMode) {
73+
document.addEventListener('scroll', this.onScroll, {
74+
passive: false
75+
})
76+
77+
// taking root offsetTop or offsetLeft as slot header size
78+
const { root } = this.$refs
79+
if (root) {
80+
this.virtual.updateParam('slotHeaderSize', root[this.isHorizontal ? 'offsetLeft' : 'offsetTop'])
81+
}
82+
}
7083
},
7184

7285
beforeDestroy () {
@@ -86,27 +99,45 @@ const VirtualList = Vue.component('virtual-list', {
8699

87100
// return current scroll offset
88101
getOffset () {
89-
const { root } = this.$refs
90-
return root ? Math.ceil(root[this.directionKey]) : 0
102+
if (this.pageMode) {
103+
return document.documentElement[this.directionKey]
104+
} else {
105+
const { root } = this.$refs
106+
return root ? Math.ceil(root[this.directionKey]) : 0
107+
}
91108
},
92109

93110
// return client viewport size
94111
getClientSize () {
95-
const { root } = this.$refs
96-
return root ? root[this.isHorizontal ? 'clientWidth' : 'clientHeight'] : 0
112+
const key = this.isHorizontal ? 'clientWidth' : 'clientHeight'
113+
if (this.pageMode) {
114+
return document.documentElement[key]
115+
} else {
116+
const { root } = this.$refs
117+
return root ? root[key] : 0
118+
}
97119
},
98120

99121
// return all scroll size
100122
getScrollSize () {
101-
const { root } = this.$refs
102-
return root ? root[this.isHorizontal ? 'scrollWidth' : 'scrollHeight'] : 0
123+
const key = this.isHorizontal ? 'scrollWidth' : 'scrollHeight'
124+
if (this.pageMode) {
125+
return document.documentElement[key]
126+
} else {
127+
const { root } = this.$refs
128+
return root ? root[key] : 0
129+
}
103130
},
104131

105132
// set current scroll position to a expectant offset
106133
scrollToOffset (offset) {
107-
const { root } = this.$refs
108-
if (root) {
109-
root[this.directionKey] = offset || 0
134+
if (this.pageMode) {
135+
document.documentElement[this.directionKey] = offset
136+
} else {
137+
const { root } = this.$refs
138+
if (root) {
139+
root[this.directionKey] = offset
140+
}
110141
}
111142
},
112143

@@ -256,14 +287,14 @@ const VirtualList = Vue.component('virtual-list', {
256287
render (h) {
257288
const { header, footer } = this.$slots
258289
const { padFront, padBehind } = this.range
259-
const { isHorizontal, rootTag, wrapTag, wrapClass, wrapStyle, headerTag, headerClass, headerStyle, footerTag, footerClass, footerStyle } = this
290+
const { isHorizontal, pageMode, rootTag, wrapTag, wrapClass, wrapStyle, headerTag, headerClass, headerStyle, footerTag, footerClass, footerStyle } = this
260291
const paddingStyle = { padding: isHorizontal ? `0px ${padBehind}px 0px ${padFront}px` : `${padFront}px 0px ${padBehind}px` }
261292
const wrapperStyle = wrapStyle ? Object.assign({}, wrapStyle, paddingStyle) : paddingStyle
262293

263294
return h(rootTag, {
264295
ref: 'root',
265296
on: {
266-
'&scroll': this.onScroll
297+
'&scroll': !pageMode && this.onScroll
267298
}
268299
}, [
269300
// header slot

src/props.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export const VirtualProps = {
4848
type: Number,
4949
default: 0
5050
},
51+
pageMode: {
52+
type: Boolean,
53+
default: false
54+
},
5155
rootTag: {
5256
type: String,
5357
default: 'div'

src/virtual.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default class Virtual {
7070

7171
// return start index offset
7272
getOffset (start) {
73-
return start < 1 ? 0 : this.getIndexOffset(start)
73+
return start < 1 ? 0 : this.getIndexOffset(start) + this.param.slotHeaderSize
7474
}
7575

7676
updateParam (key, value) {

0 commit comments

Comments
 (0)