Skip to content

Commit 690ea1e

Browse files
committed
feat: [Request] debounce property in QScrollObservable component quasarframework#2169
1 parent 340fcb7 commit 690ea1e

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

src/components/observables/QScrollObservable.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { listenOpts } from '../../utils/event'
33

44
export default {
55
name: 'QScrollObservable',
6+
props: {
7+
debounce: Number
8+
},
69
render () {},
710
data () {
811
return {
@@ -21,9 +24,14 @@ export default {
2124
inflexionPosition: this.dirChangePos
2225
}
2326
},
24-
trigger () {
25-
if (!this.timer) {
26-
this.timer = window.requestAnimationFrame(this.emit)
27+
trigger (immediately) {
28+
if (immediately || this.debounce === 0) {
29+
this.emit()
30+
}
31+
else if (!this.timer) {
32+
this.timer = this.debounce
33+
? setTimeout(this.emit, this.debounce)
34+
: requestAnimationFrame(this.emit)
2735
}
2836
},
2937
emit () {
@@ -46,9 +54,11 @@ export default {
4654
mounted () {
4755
this.target = getScrollTarget(this.$el.parentNode)
4856
this.target.addEventListener('scroll', this.trigger, listenOpts.passive)
49-
this.trigger()
57+
this.trigger(true)
5058
},
5159
beforeDestroy () {
60+
clearTimeout(this.timer)
61+
cancelAnimationFrame(this.timer)
5262
this.target.removeEventListener('scroll', this.trigger, listenOpts.passive)
5363
}
5464
}

src/components/pull-to-refresh/QPullToRefresh.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ export default {
112112
this.pullPosition -= (this.pullPosition - target) / 7
113113

114114
if (this.pullPosition - target > 1) {
115-
this.animating = window.requestAnimationFrame(() => {
115+
this.animating = requestAnimationFrame(() => {
116116
this.__animateTo(target, done, true)
117117
})
118118
}
119119
else {
120-
this.animating = window.requestAnimationFrame(() => {
120+
this.animating = requestAnimationFrame(() => {
121121
this.pullPosition = target
122122
this.animating = false
123123
done && done()

src/utils/animate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ export function start ({name, duration = 300, to, from, apply, done, cancel, eas
3434
pos: newPos,
3535
progress
3636
}
37-
anim.timer = window.requestAnimationFrame(handler)
37+
anim.timer = requestAnimationFrame(handler)
3838
}
3939

4040
const anim = ids[id] = {
4141
cancel,
42-
timer: window.requestAnimationFrame(handler)
42+
timer: requestAnimationFrame(handler)
4343
}
4444

4545
return id

src/utils/debounce.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function frameDebounce (fn) {
3030
if (wait) { return }
3131

3232
wait = true
33-
frame = window.requestAnimationFrame(() => {
33+
frame = requestAnimationFrame(() => {
3434
fn.apply(this, args)
3535
wait = false
3636
})

src/utils/scroll.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function animScrollTo (el, to, duration) {
2222

2323
const pos = getScrollPosition(el)
2424

25-
window.requestAnimationFrame(() => {
25+
requestAnimationFrame(() => {
2626
setScroll(el, pos + (to - pos) / duration * 16)
2727
if (el.scrollTop !== to) {
2828
animScrollTo(el, to, duration - 16)

0 commit comments

Comments
 (0)