/
browserSwipe.js
74 lines (62 loc) · 1.73 KB
/
browserSwipe.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
(function($){
$.browserSwipe = function(options) {
var listening = true;
var checkDelta = false;
var defaults = {
up: function(){ console.log('up') },
down: function(){ console.log('down') }
}
var settings = $.extend(defaults, options);
function _constructor() {
_bindEvents();
}
function _bindEvents() {
$(window).on('mousewheel', _throttle(_checkSwipe, 250));
$(window).on('DOMMouseScroll', _throttle(_checkSwipe, 250));
}
function _throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}
function _checkSwipe(event){
if(listening == true){
if(checkDelta == false) {
setTimeout(function(){
listening = false;
checkDelta = true;
},100);
}
} else {
if(checkDelta == true) {
if((event.originalEvent.wheelDeltaY ? (event.originalEvent.wheelDeltaY > 0) : (event.originalEvent.detail) < 0)) {
settings.up.call();
} else {
settings.down.call();
}
checkDelta = false;
setTimeout(function(){
listening = true;
}, 1000);
}
}
}
return _constructor();
};
})(jQuery);