-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (69 loc) 路 1.39 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* hoverThrottle
* - Keeps your heavy hover states from janking up scroll
*/
var extend = require('extend');
var raf = require('raf');
var options = {
'debounce' : 100
};
var data = {
'ticking' : false,
'throttle' : null
};
/**
* Merge the options and start listening to the events
*/
var init = function(opts) {
options = extend(true, options, opts);
on();
};
/**
* Requests a new animation frame if we're not already animating
*/
var scroll = function() {
if ( ! data.ticking ) raf(update);
data.ticking = true;
};
/**
* Disable cursor events, queue the throttle
* and disable ticking when finished
*/
var update = function() {
enabled();
clearTimeout(data.throttle);
data.throttle = setTimeout(disabled, options.debounce);
data.ticking = false;
};
/**
* Disable the pointer event prevention
*/
var disabled = function() {
document.querySelector('html').style.removeProperty('pointer-events');
};
/**
* Enable the pointer event prevention
*/
var enabled = function() {
document.querySelector('html').style['pointer-events'] = 'none';
};
/**
* Listen to scroll events
*/
var on = function() {
window.addEventListener('scroll', scroll, false);
};
/**
* Stop listening to scroll events
*/
var off = function() {
window.removeEventListener('scroll', scroll, false);
};
/**
* Public methods
*/
module.exports = {
init : init,
on : on,
off : off
}