-
Notifications
You must be signed in to change notification settings - Fork 17
/
ScrollListenerMixin.js
62 lines (49 loc) · 1.37 KB
/
ScrollListenerMixin.js
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
'use strict';
var win = typeof window !== 'undefined' ? window : false;
var ViewportMetrics = require('react/lib/ViewportMetrics');
var ScrollListenerMixin = {
getDefaultProps: function () {
return {
endScrollTimeout: 300
};
},
getInitialState: function () {
return {
scrollTop: 0,
isScrolling: false
};
},
componentDidMount: function () {
if (win) {
win.addEventListener('scroll', this._onPageScroll);
}
},
componentWillUnmount: function () {
if (win) {
win.removeEventListener('scroll', this._onPageScroll);
}
},
_onPageScrollEnd: function () {
var scrollTop = ViewportMetrics.currentScrollTop;
if (scrollTop === this.state.scrollTop) {
win.clearTimeout(this._pageScrollTimeout);
this.setState({ isScrolling: false });
if (typeof this.onPageScrollEnd === 'function') {
this.onPageScrollEnd(scrollTop);
}
}
},
_onPageScroll: function () {
var scrollTop = ViewportMetrics.currentScrollTop;
this.setState({
scrollTop: scrollTop,
isScrolling: true
});
win.clearTimeout(this._pageScrollTimeout);
this._pageScrollTimeout = win.setTimeout(this._onPageScrollEnd, this.props.endScrollTimeout);
if (typeof this.onPageScroll === 'function') {
this.onPageScroll(scrollTop);
}
}
};
module.exports = ScrollListenerMixin;