mui.fixed.fastclick.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * fastclick(only for radio,checkbox)
  3. */
  4. (function($, window, name) {
  5. if (!$.os.android && !$.os.ios) { //目前仅识别android和ios
  6. return;
  7. }
  8. if (window.FastClick) {
  9. return;
  10. }
  11. var handle = function(event, target) {
  12. if (target.tagName === 'LABEL') {
  13. if (target.parentNode) {
  14. target = target.parentNode.querySelector('input');
  15. }
  16. }
  17. if (target && (target.type === 'radio' || target.type === 'checkbox')) {
  18. if (!target.disabled) { //disabled
  19. return target;
  20. }
  21. }
  22. return false;
  23. };
  24. $.registerTarget({
  25. name: name,
  26. index: 40,
  27. handle: handle,
  28. target: false
  29. });
  30. var dispatchEvent = function(event) {
  31. var targetElement = $.targets.click;
  32. if (targetElement) {
  33. var clickEvent, touch;
  34. // On some Android devices activeElement needs to be blurred otherwise the synthetic click will have no effect
  35. if (document.activeElement && document.activeElement !== targetElement) {
  36. document.activeElement.blur();
  37. }
  38. touch = event.detail.gesture.changedTouches[0];
  39. // Synthesise a click event, with an extra attribute so it can be tracked
  40. clickEvent = document.createEvent('MouseEvents');
  41. clickEvent.initMouseEvent('click', true, true, window, 1, touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null);
  42. clickEvent.forwardedTouchEvent = true;
  43. targetElement.dispatchEvent(clickEvent);
  44. event.detail && event.detail.gesture.preventDefault();
  45. }
  46. };
  47. window.addEventListener('tap', dispatchEvent);
  48. window.addEventListener('doubletap', dispatchEvent);
  49. //捕获
  50. window.addEventListener('click', function(event) {
  51. if ($.targets.click) {
  52. if (!event.forwardedTouchEvent) { //stop click
  53. if (event.stopImmediatePropagation) {
  54. event.stopImmediatePropagation();
  55. } else {
  56. // Part of the hack for browsers that don't support Event#stopImmediatePropagation
  57. event.propagationStopped = true;
  58. }
  59. event.stopPropagation();
  60. event.preventDefault();
  61. return false;
  62. }
  63. }
  64. }, true);
  65. })(mui, window, 'click');