forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQScrollObservable.js
More file actions
64 lines (61 loc) · 1.55 KB
/
QScrollObservable.js
File metadata and controls
64 lines (61 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { getScrollPosition, getScrollTarget } from '../../utils/scroll.js'
import { listenOpts } from '../../utils/event.js'
export default {
name: 'QScrollObservable',
props: {
debounce: Number
},
render () {},
data () {
return {
pos: 0,
dir: 'down',
dirChanged: false,
dirChangePos: 0
}
},
methods: {
getPosition () {
return {
position: this.pos,
direction: this.dir,
directionChanged: this.dirChanged,
inflexionPosition: this.dirChangePos
}
},
trigger (immediately) {
if (immediately === true || this.debounce === 0) {
this.emit()
}
else if (!this.timer) {
this.timer = this.debounce
? setTimeout(this.emit, this.debounce)
: requestAnimationFrame(this.emit)
}
},
emit () {
const
pos = Math.max(0, getScrollPosition(this.target)),
delta = pos - this.pos,
dir = delta < 0 ? 'up' : 'down'
this.dirChanged = this.dir !== dir
if (this.dirChanged) {
this.dir = dir
this.dirChangePos = this.pos
}
this.timer = null
this.pos = pos
this.$emit('scroll', this.getPosition())
}
},
mounted () {
this.target = getScrollTarget(this.$el.parentNode)
this.target.addEventListener('scroll', this.trigger, listenOpts.passive)
this.trigger(true)
},
beforeDestroy () {
clearTimeout(this.timer)
cancelAnimationFrame(this.timer)
this.target.removeEventListener('scroll', this.trigger, listenOpts.passive)
}
}