z-paging-renderjs.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // [z-paging]使用renderjs在app-vue和h5中对touchmove事件冒泡进行处理
  2. import u from '../js/z-paging-utils'
  3. const 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, { passive: true })
  28. window.addEventListener('touchmove', this._handleTouchmove, { passive: false })
  29. }
  30. },
  31. _handleTouchstart(e) {
  32. const touch = u.getTouch(e);
  33. data.startY = touch.touchY;
  34. const touchResult = u.getTouchFromZPaging(e.target);
  35. data.isTouchFromZPaging = touchResult.isFromZp;
  36. data.isUsePageScroll = touchResult.isPageScroll;
  37. data.isReachedTop = touchResult.isReachedTop;
  38. },
  39. _handleTouchmove(e) {
  40. const touch = u.getTouch(e);
  41. const moveY = touch.touchY - data.startY;
  42. if (data.isTouchFromZPaging && ((data.isReachedTop && moveY > 0) || (data.isIosAndH5 && !data.isUsePageScroll && moveY < 0))) {
  43. if (e.cancelable && !e.defaultPrevented) {
  44. e.preventDefault();
  45. }
  46. }
  47. },
  48. _removeAllEventListener(){
  49. window.removeEventListener('touchstart');
  50. window.removeEventListener('touchmove');
  51. }
  52. }
  53. };