carto.js/vendor/mwheelIntent.js

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-06-13 18:34:34 +08:00
/**
* @author trixta and bodrovis
* @version 1.3
*/
(function($){
var mwheelI = {
pos: [-260, -260]
},
minDif = 3,
doc = document,
root = doc.documentElement,
body = doc.body,
longDelay, shortDelay
;
function unsetPos(){
if(this === mwheelI.elem){
mwheelI.pos = [-260, -260];
mwheelI.elem = false;
minDif = 3;
}
}
$.event.special.mwheelIntent = {
setup: function() {
var jElm = $(this).on('mousewheel', $.event.special.mwheelIntent.handler);
if( this !== doc && this !== root && this !== body ){
jElm.on('mouseleave', unsetPos);
}
jElm = null;
return true;
},
teardown: function(){
$(this)
.off('mousewheel', $.event.special.mwheelIntent.handler)
.off('mouseleave', unsetPos)
;
return true;
},
handler: function(e, d) {
var pos = [e.clientX, e.clientY];
if( this === mwheelI.elem || Math.abs(mwheelI.pos[0] - pos[0]) > minDif ||
Math.abs(mwheelI.pos[1] - pos[1]) > minDif ) {
mwheelI.elem = this;
mwheelI.pos = pos;
minDif = 250;
clearTimeout(shortDelay);
shortDelay = setTimeout(function(){
minDif = 10;
}, 200);
clearTimeout(longDelay);
longDelay = setTimeout(function(){
minDif = 3;
}, 1500);
e = $.extend({}, e, {type: 'mwheelIntent'});
return $.event.dispatch.apply(this, arguments);
}
}
};
$.fn.extend({
mwheelIntent: function(fn) {
return fn ? this.on("mwheelIntent", fn) : this.trigger("mwheelIntent");
},
unmwheelIntent: function(fn) {
return this.off("mwheelIntent", fn);
}
});
$(function(){
body = doc.body;
//assume that document is always scrollable, doesn't hurt if not
$(doc).on('mwheelIntent.mwheelIntentDefault', $.noop);
});
})(jQuery);