onTouchStart.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {
  2. now
  3. } from '../../shared/utils.js';
  4. export default function onTouchStart(event) {
  5. const swiper = this;
  6. const data = swiper.touchEventsData;
  7. const {
  8. params,
  9. touches,
  10. enabled
  11. } = swiper;
  12. if (!enabled) return;
  13. if (swiper.animating && params.preventInteractionOnTransition) {
  14. return;
  15. }
  16. if (!swiper.animating && params.cssMode && params.loop) {
  17. swiper.loopFix();
  18. }
  19. let e = event;
  20. if (e.originalEvent) e = e.originalEvent;
  21. data.isTouchEvent = e.type === 'touchstart' || e.type === 'touchStart' || e.type === 'onTouchstart';
  22. if (!data.isTouchEvent && 'which' in e && e.which === 3) return;
  23. if (!data.isTouchEvent && 'button' in e && e.button > 0) return;
  24. if (data.isTouched && data.isMoved) return; // change target el for shadow root component
  25. const swipingClassHasValue = !!params.noSwipingClass && params.noSwipingClass !== '';
  26. const noSwipingSelector = params.noSwipingSelector ? params.noSwipingSelector : `.${params.noSwipingClass}`;
  27. const isTargetShadow = !!(e.target && e.target
  28. .shadowRoot
  29. );
  30. if (params.noSwiping) {
  31. swiper.allowClick = true;
  32. return;
  33. }
  34. if (params.swipeHandler) {
  35. if (!$targetEl.closest(params.swipeHandler)[0]) return;
  36. }
  37. touches.currentX = (e.type === 'touchstart' || e.type === 'touchStart' || e.type === 'onTouchstart') ? e.touches[0]
  38. .pageX : e.pageX;
  39. touches.currentY = (e.type === 'touchstart' || e.type === 'touchStart' || e.type === 'onTouchstart') ? e.touches[0]
  40. .pageY : e.pageY;
  41. const startX = touches.currentX;
  42. const startY = touches
  43. .currentY;
  44. const edgeSwipeDetection = params.edgeSwipeDetection || params.iOSEdgeSwipeDetection;
  45. const edgeSwipeThreshold = params.edgeSwipeThreshold || params.iOSEdgeSwipeThreshold;
  46. Object.assign(data, {
  47. isTouched: true,
  48. isMoved: false,
  49. allowTouchCallbacks: true,
  50. isScrolling: undefined,
  51. startMoving: undefined
  52. });
  53. touches.startX = startX;
  54. touches.startY = startY;
  55. data.touchStartTime = now();
  56. swiper.allowClick = true;
  57. swiper.updateSize();
  58. swiper.swipeDirection = undefined;
  59. if (params.threshold > 0) data.allowThresholdMove = false;
  60. // if (e.type !== 'touchstart' && e.type !== 'touchStart') {
  61. // let preventDefault = true;
  62. // if ($targetEl.is(data.focusableElements)) preventDefault = false;
  63. // const shouldPreventDefault = preventDefault && swiper.allowTouchMove && params.touchStartPreventDefault;
  64. // if ((params.touchStartForcePreventDefault || shouldPreventDefault) && !$targetEl[0].isContentEditable) {
  65. // e.preventDefault();
  66. // }
  67. // }
  68. swiper.emit('touch-start', e);
  69. }