Skip to content

Commit f87abda

Browse files
committed
feat: support step option for throttle
1 parent 811616c commit f87abda

File tree

6 files changed

+27
-0
lines changed

6 files changed

+27
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ Available `Prop` :
131131
| remain | Number | * | The number of item that show in view port.(default `10`) |
132132
| keep | Boolean | * | Work with `keep-alive` component,keep scroll position after activated.(default `false`) |
133133
| enabled | Boolean | * | If you want to render all data directly,please set 'false' for this option.But `toTop``toBottom` and `scrolling` event is still available.(default `true`) |
134+
| debounce | Number | * | Milliseconds of using debounce function to ensure scroll event doesn't fire so often.(disabled by default) |
135+
| step | Number | * | Pixel of using throttle theory to decrease the frequency of scroll event.(disabled by default) |
134136

135137
Available `Event` :
136138

dist/vue-scroll-list.common.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ var component = {
3333
},
3434
debounce: {
3535
type: Number
36+
},
37+
step: { // throttle
38+
type: Number
3639
}
3740
},
3841
methods: {
3942
handleScroll: function handleScroll(event) {
4043
var scrollTop = this.$el.scrollTop;
44+
if (!this.ignoreStep && this.step && Math.abs(scrollTop - this.scrollTop) < this.step) return;
45+
this.ignoreStep = false;
4146
this.scrollTop = scrollTop;
4247
this.$emit('scrolling', event);
4348
this.updateZone(scrollTop);
@@ -171,6 +176,7 @@ var component = {
171176
activated: function activated() {
172177
// while work with keep-alive component
173178
// set scroll position after 'activated'
179+
this.ignoreStep = true;
174180
this.$el.scrollTop = this.keep ? this.scrollTop || 1 : 1;
175181
},
176182
render: function render(h) {

dist/vue-scroll-list.esm.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ var component = {
3131
},
3232
debounce: {
3333
type: Number
34+
},
35+
step: { // throttle
36+
type: Number
3437
}
3538
},
3639
methods: {
3740
handleScroll: function handleScroll(event) {
3841
var scrollTop = this.$el.scrollTop;
42+
if (!this.ignoreStep && this.step && Math.abs(scrollTop - this.scrollTop) < this.step) return;
43+
this.ignoreStep = false;
3944
this.scrollTop = scrollTop;
4045
this.$emit('scrolling', event);
4146
this.updateZone(scrollTop);
@@ -169,6 +174,7 @@ var component = {
169174
activated: function activated() {
170175
// while work with keep-alive component
171176
// set scroll position after 'activated'
177+
this.ignoreStep = true;
172178
this.$el.scrollTop = this.keep ? this.scrollTop || 1 : 1;
173179
},
174180
render: function render(h) {

dist/vue-scroll-list.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ var component = {
3737
},
3838
debounce: {
3939
type: Number
40+
},
41+
step: { // throttle
42+
type: Number
4043
}
4144
},
4245
methods: {
4346
handleScroll: function handleScroll(event) {
4447
var scrollTop = this.$el.scrollTop;
48+
if (!this.ignoreStep && this.step && Math.abs(scrollTop - this.scrollTop) < this.step) return;
49+
this.ignoreStep = false;
4550
this.scrollTop = scrollTop;
4651
this.$emit('scrolling', event);
4752
this.updateZone(scrollTop);
@@ -175,6 +180,7 @@ var component = {
175180
activated: function activated() {
176181
// while work with keep-alive component
177182
// set scroll position after 'activated'
183+
this.ignoreStep = true;
178184
this.$el.scrollTop = this.keep ? this.scrollTop || 1 : 1;
179185
},
180186
render: function render(h) {

example/componentA.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:remain="10"
77
:enabled="true"
88
:keep="true"
9+
:step="5"
910
@toTop="onTop"
1011
@toBottom="onBottom"
1112
@scrolling="onScroll">

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ const component = {
2828
},
2929
debounce: {
3030
type: Number
31+
},
32+
step: { // throttle
33+
type: Number
3134
}
3235
},
3336
methods: {
3437
handleScroll(event) {
3538
const scrollTop = this.$el.scrollTop;
39+
if (!this.ignoreStep && this.step && Math.abs(scrollTop - this.scrollTop) < this.step) return;
40+
this.ignoreStep = false;
3641
this.scrollTop = scrollTop;
3742
this.$emit('scrolling', event);
3843
this.updateZone(scrollTop);
@@ -160,6 +165,7 @@ const component = {
160165
activated() {
161166
// while work with keep-alive component
162167
// set scroll position after 'activated'
168+
this.ignoreStep = true;
163169
this.$el.scrollTop = this.keep ? (this.scrollTop || 1) : 1;
164170
},
165171
render(h) {

0 commit comments

Comments
 (0)