Skip to content

Commit 75542d2

Browse files
committed
Add debounce support.
1 parent 3a1d49c commit 75542d2

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,14 @@ new Vue({
104104

105105
## Props type
106106

107+
<img height="180" src="https://tangbc.github.io/github-images/vitual-scroll-list-3.png">
108+
107109
*Prop* | *Type* | *Required* | *Description* |
108110
:--- | :--- | :--- | :--- |
109111
| size | Number || Each list item height, currently only supports fixed height. |
110112
| remain | Number || How many items should be shown in virtual-list viewport, so `size` and `remain` will determine the virtual-list outside container height (size × remian). |
111113
| start | Number | * | Default value is `0`, the initial scroll start index. It must be integer and in the range of list index, throws a warning if index does not exist. |
114+
| debounce | Number | * | Milliseconds of using `debounce` function to ensure scroll event doesn't fire so often that it bricks browser performance, it's disabled by default. |
112115
| rtag | String | * | Default value is `div`, the virtual-list's root HTMLElement tag name, in all case it's style is set to `display: block;` |
113116
| rclass | String | * | Default value is an empty string, the virtual-list's root HTMLElement tag's classes. Has the same API has [`v-bind:class`](https://vuejs.org/v2/guide/class-and-style.html) |
114117
| wtag | String | * | Default value is `div`, the virtual-list's item wrapper HTMLElement tag name, in all case it's style is set to `display: block;` |

index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@
1515

1616
var innerns = 'vue-virtual-scroll-list'
1717

18+
var _debounce = function (func, wait, immediate) {
19+
var timeout
20+
return function () {
21+
var context = this
22+
var args = arguments
23+
var later = function () {
24+
timeout = null
25+
if (!immediate) {
26+
func.apply(context, args)
27+
}
28+
}
29+
var callNow = immediate && !timeout
30+
clearTimeout(timeout)
31+
timeout = setTimeout(later, wait)
32+
if (callNow) {
33+
func.apply(context, args)
34+
}
35+
}
36+
}
37+
1838
return Vue2.component(innerns, {
1939
props: {
2040
size: { type: Number, required: true },
@@ -24,6 +44,7 @@
2444
wtag: { type: String, default: 'div' },
2545
wclass: { type: String, default: '' },
2646
start: { type: Number, default: 0 },
47+
debounce: { type: Number, default: 0 },
2748
totop: Function,
2849
tobottom: Function,
2950
onscroll: Function
@@ -207,6 +228,7 @@
207228
render: function (createElement) {
208229
var showList = this.filter(this.$slots.default)
209230
var delta = this.delta
231+
var dbc = this.debounce
210232

211233
return createElement(this.rtag, {
212234
'ref': 'container',
@@ -216,7 +238,7 @@
216238
'height': delta.viewHeight + 'px'
217239
},
218240
'on': {
219-
'scroll': this.handleScroll
241+
'scroll': dbc ? _debounce(this.handleScroll.bind(this), dbc) : this.handleScroll
220242
},
221243
'class': this.rclass
222244
}, [

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.3",
3+
"version": "1.1.4",
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)