Skip to content

Commit d70bbf6

Browse files
committed
Merge pull request jordansinger#14 from paulmillr/patch-1
Properly manage handlers.
2 parents 11715eb + f858c87 commit d70bbf6

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

hook.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
1515
return !!('ontouchstart' in window) || !!('onmsgesturechange' in window);
1616
};
1717

18+
var handlers = {};
19+
20+
var addHandler = function(name, fn) {
21+
win.on(name, fn);
22+
handlers[name] = fn;
23+
};
24+
var removeHandler = function(name, fn) {
25+
win.off(name, handlers[name]);
26+
delete handlers[name];
27+
};
28+
var removeHandlers = function() {
29+
for (var name in handlers) {
30+
removeHandler(name);
31+
}
32+
};
1833

1934
var methods = {
2035

@@ -64,22 +79,22 @@
6479

6580
if(!hasTouch()) {
6681
if(settings.scrollWheelSelected === true){
67-
win.on('mousewheel', function(event, delta) {
82+
addHandler('mousewheel', function(event, delta) {
6883
methods.onScroll($this, settings, delta);
69-
});
84+
})
7085
} else {
71-
win.on('scroll', function() {
86+
addHandler('scroll', function() {
7287
methods.onScroll($this, settings);
7388
});
7489
}
7590
} else {
7691
var lastY = 0,
7792
swipe = 0;
78-
win.on('touchstart', function(e){
93+
addHandler('touchstart', function(e){
7994
lastY = e.originalEvent.touches[0].pageY;
8095
});
8196

82-
win.on('touchmove', function(e) {
97+
addHandler('touchmove', function(e) {
8398
swipe = e.originalEvent.touches[0].pageY + lastY;
8499
st = $(this).scrollTop();
85100

@@ -92,7 +107,7 @@
92107
}
93108
});
94109

95-
win.on('touchend', function(){
110+
addHandler('touchend', function(){
96111
swipe = 0;
97112
});
98113
}
@@ -143,6 +158,7 @@
143158
},
144159

145160
destroy: function() {
161+
removeHandlers();
146162
return $(this).each(function(){
147163
var $this = $(this);
148164

@@ -168,4 +184,4 @@
168184
return method.apply(this, arguments);
169185
};
170186

171-
})( jQuery, window, document );
187+
})( jQuery, window, document );

0 commit comments

Comments
 (0)