forked from dcloudio/mui
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmui.fixed.fastclick.js
68 lines (65 loc) · 2 KB
/
mui.fixed.fastclick.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
/**
* fastclick(only for radio,checkbox)
*/
(function($, window, name) {
if (!$.os.android && !$.os.ios) { //目前仅识别android和ios
return;
}
if (window.FastClick) {
return;
}
var handle = function(event, target) {
if (target.tagName === 'LABEL') {
if (target.parentNode) {
target = target.parentNode.querySelector('input');
}
}
if (target && (target.type === 'radio' || target.type === 'checkbox')) {
if (!target.disabled) { //disabled
return target;
}
}
return false;
};
$.registerTarget({
name: name,
index: 40,
handle: handle,
target: false
});
var dispatchEvent = function(event) {
var targetElement = $.targets.click;
if (targetElement) {
var clickEvent, touch;
// On some Android devices activeElement needs to be blurred otherwise the synthetic click will have no effect
if (document.activeElement && document.activeElement !== targetElement) {
document.activeElement.blur();
}
touch = event.detail.gesture.changedTouches[0];
// Synthesise a click event, with an extra attribute so it can be tracked
clickEvent = document.createEvent('MouseEvents');
clickEvent.initMouseEvent('click', true, true, window, 1, touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null);
clickEvent.forwardedTouchEvent = true;
targetElement.dispatchEvent(clickEvent);
event.detail && event.detail.gesture.preventDefault();
}
};
window.addEventListener('tap', dispatchEvent);
window.addEventListener('doubletap', dispatchEvent);
//捕获
window.addEventListener('click', function(event) {
if ($.targets.click) {
if (!event.forwardedTouchEvent) { //stop click
if (event.stopImmediatePropagation) {
event.stopImmediatePropagation();
} else {
// Part of the hack for browsers that don't support Event#stopImmediatePropagation
event.propagationStopped = true;
}
event.stopPropagation();
event.preventDefault();
return false;
}
}
}, true);
})(mui, window, 'click');