index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import onTouchStart from './onTouchStart.js';
  2. import onTouchMove from './onTouchMove.js';
  3. import onTouchEnd from './onTouchEnd.js';
  4. import onResize from './onResize.js';
  5. import onClick from './onClick.js';
  6. import onScroll from './onScroll.js';
  7. let dummyEventAttached = false;
  8. function dummyEventListener() {}
  9. const events = (swiper, method) => {
  10. const {
  11. params,
  12. touchEvents,
  13. wrapperEl,
  14. device,
  15. support
  16. } = swiper;
  17. let el = swiper.native;
  18. const capture = !!params.nested;
  19. const domMethod = method === 'on' ? 'on' : 'off';
  20. const swiperMethod = method;
  21. if (!support.touch) {
  22. let desktopMethod = method === 'on' ? 'addEventListener' : 'removeEventListener';
  23. if (document.querySelector(`#${swiper.$el.swiperElId}`)) {
  24. document.querySelector(`#${swiper.$el.swiperElId}`)[desktopMethod](touchEvents.start, swiper
  25. .onTouchStart,
  26. false);
  27. }
  28. document[desktopMethod](touchEvents.move, swiper.onTouchMove, capture);
  29. document[desktopMethod](touchEvents.end, swiper.onTouchEnd, false);
  30. } else {
  31. const passiveListener = touchEvents.start === 'touchstart' && support.passiveListener && params
  32. .passiveListeners ? {
  33. passive: true,
  34. capture: false
  35. } : false;
  36. }
  37. };
  38. function attachEvents() {
  39. const swiper = this;
  40. const {
  41. params,
  42. support
  43. } = swiper;
  44. swiper.onTouchStart = onTouchStart.bind(swiper);
  45. swiper.onTouchMove = onTouchMove.bind(swiper);
  46. swiper.onTouchEnd = onTouchEnd.bind(swiper);
  47. if (params.cssMode) {
  48. swiper.onScroll = onScroll.bind(swiper);
  49. }
  50. swiper.onClick = onClick.bind(swiper);
  51. events(swiper, 'on');
  52. }
  53. function detachEvents() {
  54. const swiper = this;
  55. events(swiper, 'off');
  56. }
  57. export default {
  58. attachEvents,
  59. detachEvents
  60. };