Skip to content

Commit 20de341

Browse files
committed
Add offset prop to support assign scroll offset.
1 parent 47db9d8 commit 20de341

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
<a href="http://packagequality.com/#?package=vue-virtual-scroll-list">
1111
<img src="http://npm.packagequality.com/shield/vue-virtual-scroll-list.svg">
1212
</a>
13-
<a href="https://github.com/tangbc/vue-virtual-scroll-list/blob/master/LICENSE">
14-
<img src="https://img.shields.io/github/license/tangbc/vue-virtual-scroll-list.svg" alt="MIT License"/>
15-
</a>
1613

1714

1815
## vue-virtual-scroll-list
@@ -25,18 +22,18 @@
2522

2623
* Support fixed height and variable height.
2724

28-
* Support set the initial scroll index or change any.
25+
* Support set the initial scroll index or offset.
2926

30-
* Event scrolling, reach top and bottom can be detected.
27+
* Event scroll, reach top and bottom can be detected.
3128

3229

3330
## Live demos
3431

3532
* [vue-virtual-scroll-list with 100,000 finite data](https://tangbc.github.io/vue-virtual-scroll-list/examples/finite/).
3633

37-
* [vue-virtual-scroll-list with requesting infinite data](https://tangbc.github.io/vue-virtual-scroll-list/examples/infinite/).
34+
* [vue-virtual-scroll-list with request infinite data](https://tangbc.github.io/vue-virtual-scroll-list/examples/infinite/).
3835

39-
* [vue-virtual-scroll-list with variable height](https://tangbc.github.io/vue-virtual-scroll-list/examples/variable/).
36+
* [vue-virtual-scroll-list with variable height mode](https://tangbc.github.io/vue-virtual-scroll-list/examples/variable/).
4037

4138

4239
## How it works
@@ -118,15 +115,16 @@ new Vue({
118115
:--- | :--- | :--- | :--- |
119116
| size | Number || Each list item height, in variable height mode, this prop just use to calculate the virtual-list outside container viewport height. |
120117
| remain | Number || How many items should be shown in virtual-list viewport, so `size` and `remain` determine the outside container viewport height (size × remian). |
121-
| start | Number | * | Default value is `0`, the initial scroll start index. It must be integer and in the range of list index, if out of range it will be turned to `0` or the last one. |
118+
| start | Number | * | Default value is `0`, the initial scroll start index. It must be integer and in the range of list index, if invalid there will be effected as `0` or the last one. |
119+
| offset | Number | * | Default value is `0`, the initial scroll offset. If both `start` and `offset` are assigned at initialization, `start` is preferred. |
122120
| bench | Number | * | Default value is equal to `remain`, unreached items count, not show in virtual-list viewport but exist in real DOM, the larger the bench, the higher the scroll performance will achieved. |
123121
| debounce | Number | * | **It's disabled by default**, milliseconds of using `debounce` function to ensure scroll event doesn't fire so often that it bricks browser performance. |
124122
| rtag | String | * | Default value is `div`, the virtual-list root element tag name, in all cases it's style is set to `display: block;` |
125123
| wtag | String | * | Default value is `div`, the virtual-list item wrapper element tag name, in all cases it's style is set to `display: block;` |
126124
| wclass | String | * | Default value is an empty string, the virtual-list item wrapper element class, if assign this prop, you better **not** to change it's [CSS box model](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model). |
127125
| totop | Function | * | Called when virtual-list is scrolled to top, no param. |
128126
| tobottom | Function | * | Called when virtual-list is scrolled to bottom, no param. |
129-
| onscroll | Function | * | Called when virtual-list is scrolling, param: `(e, { offset, offsetAll, start, end })`. |
127+
| onscroll | Function | * | Called when virtual-list is scrolling, with param: [`(event, data)`](https://github.com/tangbc/vue-virtual-scroll-list/releases/tag/v1.1.7). |
130128
| variable | Function or Boolean | * | For using virtual-list with variable height mode. If assign `Function`, this prop is a variable height getter function which is called with param: `(index)` when each item is ready to be calculated. If assign `Boolean`, virtual-list will get each item variable height by it's inline style height automatic. |
131129

132130
### About variable height

index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
wtag: { type: String, default: 'div' },
4343
wclass: { type: String, default: '' },
4444
start: { type: Number, default: 0 },
45+
offset: { type: Number, default: 0 },
4546
variable: [Function, Boolean],
4647
bench: Number,
4748
debounce: Number,
@@ -80,6 +81,9 @@
8081
},
8182
start: function () {
8283
this.alter = 'start'
84+
},
85+
offset: function () {
86+
this.alter = 'offset'
8387
}
8488
},
8589

@@ -198,8 +202,8 @@
198202
var slot = this.$slots.default[index]
199203
var style = slot && slot.data && slot.data.style
200204
if (style && style.height) {
201-
var mc = style.height.match(/^(.*)px$/)
202-
return (mc && +mc[1]) || 0
205+
var shm = style.height.match(/^(.*)px$/)
206+
return (shm && +shm[1]) || 0
203207
}
204208
}
205209
return 0
@@ -317,6 +321,8 @@
317321
if (this.start) {
318322
var start = this.getZone(this.start).start
319323
this.setScrollTop(this.variable ? this.getVarOffset(start) : start * this.size)
324+
} else if (this.offset) {
325+
this.setScrollTop(this.offset)
320326
}
321327
},
322328

@@ -325,20 +331,20 @@
325331
var delta = this.delta
326332
delta.keeps = this.remain + (this.bench || this.remain)
327333

328-
var alterStart = this.alter === 'start'
329-
var calcStart = alterStart ? this.start : delta.start
330-
var zone = this.getZone(calcStart)
334+
var calcstart = this.alter === 'start' ? this.start : delta.start
335+
var zone = this.getZone(calcstart)
331336

332-
// if start or size change, update scroll position.
333-
if (alterStart || this.alter === 'size') {
334-
this.$nextTick(this.setScrollTop.bind(this, this.variable
335-
? this.getVarOffset(zone.isLast ? delta.total : zone.start)
336-
: zone.isLast ? delta.total * this.size : zone.start * this.size)
337+
// if start, size or offset change, update scroll position.
338+
if (~['start', 'size', 'offset'].indexOf(this.alter)) {
339+
this.$nextTick(this.setScrollTop.bind(this, this.alter === 'offset'
340+
? this.offset : this.variable
341+
? this.getVarOffset(zone.isLast ? delta.total : zone.start)
342+
: zone.isLast ? delta.total * this.size : zone.start * this.size)
337343
)
338344
}
339345

340346
// if points out difference, force update once again.
341-
if (calcStart !== zone.start || delta.end !== zone.end || this.alter) {
347+
if (calcstart !== zone.start || delta.end !== zone.end || this.alter) {
342348
this.alter = ''
343349
delta.end = zone.end
344350
delta.start = zone.start

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-virtual-scroll-list",
3-
"version": "1.1.9",
3+
"version": "1.2.0",
44
"description": "A vue (2.x) component that support big data and infinite loading by using virtual scroll list.",
55
"main": "index.js",
66
"files": [

0 commit comments

Comments
 (0)