forked from kmewhort/pointer_events_polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer_events_polyfill.js
68 lines (60 loc) · 2.26 KB
/
pointer_events_polyfill.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
/*
* Pointer Events Polyfill: Adds support for the style attribute "pointer-events: none" to browsers without this feature (namely, IE).
* (c) 2013, Kent Mewhort, licensed under BSD. See LICENSE.txt for details.
*/
// constructor
function PointerEventsPolyfill(options){
// set defaults
this.options = {
selector: '*',
mouseEvents: ['click','dblclick','mousedown','mouseup'],
usePolyfillIf: function(){
if(navigator.appName == 'Microsoft Internet Explorer')
{
var agent = navigator.userAgent;
if (agent.match(/MSIE ([0-9]{1,}[\.0-9]{0,})/) != null){
var version = parseFloat( RegExp.$1 );
if(version < 11)
return true;
}
}
return false;
}
};
if(options){
var obj = this;
jQuery.each(options, function(k,v){
obj.options[k] = v;
});
}
if(this.options.usePolyfillIf())
this.register_mouse_events();
}
// singleton initializer
PointerEventsPolyfill.initialize = function(options){
if(PointerEventsPolyfill.singleton == null)
PointerEventsPolyfill.singleton = new PointerEventsPolyfill(options);
return PointerEventsPolyfill.singleton;
};
// handle mouse events w/ support for pointer-events: none
PointerEventsPolyfill.prototype.register_mouse_events = function(){
// register on all elements (and all future elements) matching the selector
jQuery(document).on(this.options.mouseEvents.join(" "), this.options.selector, function(e){
if(jQuery(this).css('pointer-events') == 'none'){
// peak at the element below
var origDisplayAttribute = jQuery(this).css('display');
jQuery(this).css('display','none');
var underneathElem = document.elementFromPoint(e.clientX, e.clientY);
if(origDisplayAttribute)
jQuery(this)
.css('display', origDisplayAttribute);
else
jQuery(this).css('display','');
// fire the mouse event on the element below
e.target = underneathElem;
jQuery(underneathElem).trigger(e);
return false;
}
return true;
});
};