touch-events-bubbles.uvue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <scroll-view style="flex: 1">
  3. <page-head title="事件冒泡测试"></page-head>
  4. <view class="container">
  5. <view class="view-box" id="view1" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
  6. <view class="mid-view" id="view1-2" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
  7. <image class="icon" id="view1-3" src="../image/logo.png" @touchstart="onTouchStart" @touchmove="onTouchMove"
  8. @touchend="onTouchEnd"></image>
  9. </view>
  10. </view>
  11. <view class="view-box" id="view2" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
  12. <view class="mid-view" id="view2-2" @touchend="onTouchEnd">
  13. <view style="background-color: beige;" class="icon" id="view2-3" @touchstart="onTouchStart" @touchend="onTouchEnd"></view>
  14. </view>
  15. </view>
  16. </view>
  17. </scroll-view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. iconRect: null as DOMRect | null,
  24. viewEleRect: null as DOMRect | null,
  25. touchstartValue: [] as string[],
  26. touchmoveValue: [] as string[],
  27. touchendValue: [] as string[],
  28. ret1: false,
  29. ret2: false
  30. }
  31. },
  32. onReady() {
  33. // #ifdef APP-IOS || APP-HARMONY
  34. let iconEle = uni.getElementById("view1-3")
  35. this.iconRect = iconEle?.getBoundingClientRect()
  36. // 加上导航栏及状态栏高度
  37. this.iconRect.y += uni.getSystemInfoSync().safeArea.top + 44
  38. let viewEle = uni.getElementById("view2-3")
  39. this.viewEleRect = viewEle?.getBoundingClientRect()
  40. // 加上导航栏及状态栏高度
  41. this.viewEleRect.y += uni.getSystemInfoSync().safeArea.top + 44
  42. // #endif
  43. },
  44. methods: {
  45. clearValue() {
  46. this.touchstartValue = []
  47. this.touchmoveValue = []
  48. this.touchendValue = []
  49. },
  50. isPassTest1() {
  51. let touchStart = this.touchstartValue.join("")
  52. let touchMove = this.touchmoveValue.join("")
  53. let touchEnd = this.touchendValue.join("")
  54. console.log("touchStart: ", touchStart)
  55. console.log("touchMove: ", touchMove)
  56. console.log("touchEnd: ", touchEnd)
  57. let result = touchStart == "view1-3view1-2view1" &&
  58. touchMove == "view1-3view1-2view1" &&
  59. touchEnd == "view1-3view1-2view1"
  60. console.log('isPassTest1', result)
  61. this.ret1 = result
  62. this.clearValue()
  63. },
  64. isPassTest2() {
  65. let touchStart = this.touchstartValue.join("")
  66. let touchMove = this.touchmoveValue.join("")
  67. let touchEnd = this.touchendValue.join("")
  68. console.log("touchStart: ", touchStart)
  69. console.log("touchMove: ", touchMove)
  70. console.log("touchEnd: ", touchEnd)
  71. let result = touchStart == "view2-3view2" &&
  72. touchMove == "view2" &&
  73. touchEnd == "view2-3view2-2view2"
  74. console.log('isPassTest2', result)
  75. this.ret2 = result
  76. this.clearValue()
  77. },
  78. onTouchStart(e : TouchEvent) {
  79. let _id = e.currentTarget!.attributes.get("id") as string
  80. if (!this.touchstartValue.includes(_id)) {
  81. this.touchstartValue.push(_id)
  82. }
  83. console.log('onTouchStart', e.currentTarget!.attributes.get("id"))
  84. },
  85. onTouchMove(e : TouchEvent) {
  86. let _id = e.currentTarget!.attributes.get("id") as string
  87. if (!this.touchmoveValue.includes(_id)) {
  88. this.touchmoveValue.push(_id)
  89. }
  90. console.log('onTouchMove', e.currentTarget!.attributes.get("id"))
  91. },
  92. onTouchEnd(e : TouchEvent) {
  93. let _id = e.currentTarget!.attributes.get("id") as string
  94. if (!this.touchendValue.includes(_id)) {
  95. this.touchendValue.push(_id)
  96. }
  97. }
  98. }
  99. }
  100. </script>
  101. <style>
  102. .container {
  103. width: 100%;
  104. height: 80%;
  105. flex-direction: column;
  106. align-items: center;
  107. justify-content: space-between;
  108. }
  109. .view-box {
  110. width: 200px;
  111. height: 200px;
  112. align-items: center;
  113. justify-content: center;
  114. border-style: solid;
  115. }
  116. .mid-view {
  117. width: 150px;
  118. height: 150px;
  119. align-items: center;
  120. justify-content: center;
  121. background-color: aqua;
  122. }
  123. .icon {
  124. width: 100px;
  125. height: 100px;
  126. }
  127. </style>