z-paging-renderjs.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // [z-paging]使用renderjs在app-vue和h5中对touchmove事件冒泡进行处理
  2. import u from '../js/z-paging-utils'
  3. var data = {
  4. startY: 0,
  5. isTouchFromZPaging: false,
  6. isUsePageScroll: false,
  7. isReachedTop: true,
  8. isIosAndH5: false
  9. }
  10. export default {
  11. mounted() {
  12. this._handleTouch();
  13. // #ifdef APP-VUE
  14. this.$ownerInstance && this.$ownerInstance.callMethod('_checkVirtualListScroll');
  15. // #endif
  16. },
  17. methods: {
  18. //接收逻辑层发送的数据
  19. renderPropIsIosAndH5Change(newVal) {
  20. if (newVal === -1) return;
  21. data.isIosAndH5 = newVal;
  22. },
  23. //拦截处理touch事件
  24. _handleTouch() {
  25. if (window && !window.$zPagingRenderJsInited) {
  26. window.$zPagingRenderJsInited = true;
  27. window.addEventListener('touchstart', this._handleTouchstart, {
  28. passive: true
  29. })
  30. window.addEventListener('touchmove', this._handleTouchmove, {
  31. passive: false
  32. })
  33. }
  34. },
  35. _handleTouchstart(e) {
  36. const touch = u.getTouch(e);
  37. data.startY = touch.touchY;
  38. const touchResult = u.getTouchFromZPaging(e.target);
  39. data.isTouchFromZPaging = touchResult.isFromZp;
  40. data.isUsePageScroll = touchResult.isPageScroll;
  41. data.isReachedTop = touchResult.isReachedTop;
  42. },
  43. _handleTouchmove(e) {
  44. const touch = u.getTouch(e);
  45. var moveY = touch.touchY - data.startY;
  46. if (data.isTouchFromZPaging && ((data.isReachedTop && moveY > 0) || (data.isIosAndH5 && !data.isUsePageScroll && moveY < 0))) {
  47. if (e.cancelable && !e.defaultPrevented) {
  48. e.preventDefault();
  49. }
  50. }
  51. },
  52. _removeAllEventListener(){
  53. window.removeEventListener('touchstart');
  54. window.removeEventListener('touchmove');
  55. }
  56. }
  57. };