overflow-visible-event.uvue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view id="scroll-view" style="flex: 1;">
  4. <!-- #endif -->
  5. <view>
  6. <text style="font-size: 15px;">overflow=visible 父view(绿色),子view(红色),点击超出父view区域的部分也可触发事件。</text>
  7. <view class="backgroundview">
  8. <view id="parent" class="box-visible-border-radius">
  9. <view id="child" style="width: 50px; height: 150px; background-color: red;" @click="handleClickOverflowPart"
  10. @touchmove="handleTouchMoveOverflowPart" @touchstart="handleTouchStartOverflowPart"
  11. @touchend="handleTouchEndOverflowPart">
  12. </view>
  13. </view>
  14. </view>
  15. <text style="font-size: 15px;">overflow=visible组件的子view (绿色)被position:
  16. absolute的view(红色)遮挡时,点击事件测试。</text>
  17. <view style="width: 300px;height: 300px;background-color: white;">
  18. <view style="overflow:visible;height: 100px;width: 100%;">
  19. <view style="background-color: green;height: 100px;width: 100%;" @click="handleClick('green')">
  20. </view>
  21. </view>
  22. <view id="absolute-view" style="position: absolute;background-color: red;width: 100px;height: 200px;right: 0px;"
  23. @click="handleClick('red')">
  24. </view>
  25. </view>
  26. <text style="font-size: 15px;">overflow=visible组件设置hover-class的测试</text>
  27. <view class="backgroundview">
  28. <view class="box-visible-border-radius" hover-class="hover-class">
  29. <view style="background-color: red;height: 100px;width: 100px;margin-left: 20px;margin-top: 20px;">
  30. </view>
  31. </view>
  32. </view>
  33. <text style="font-size: 15px;">overflow=visible组件多层级设置测试</text>
  34. <view class="backgroundview">
  35. <view id="deep-overflow" style="overflow: visible;">
  36. <view style="overflow: visible;background-color: red;width: 100px;height: 100px;" @click="handleClick('red')">
  37. </view>
  38. </view>
  39. <view style="overflow: visible;">
  40. <view style="overflow: visible;background-color: green;width: 100px;height: 100px;">
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. <!-- #ifdef APP -->
  46. </scroll-view>
  47. <!-- #endif -->
  48. </template>
  49. <script>
  50. export default {
  51. data() {
  52. return {
  53. jest_result: false,
  54. jest_click_x: -1,
  55. jest_click_y: -1,
  56. jest_parent_top: -1.0,
  57. startX: 0,
  58. startY: 0,
  59. moveX: 0,
  60. moveY: 0,
  61. oldX: 0,
  62. oldY: 0,
  63. moveEl: null as UniElement | null
  64. }
  65. },
  66. onReady() {
  67. this.moveEl = uni.getElementById('parent')
  68. },
  69. methods: {
  70. handleClickOverflowPart() {
  71. console.log("click");
  72. this.jest_result = true;
  73. uni.showToast({ "title": "点击红色区域" })
  74. },
  75. handleTouchStartOverflowPart(e : UniTouchEvent) {
  76. this.startX = e.changedTouches[0].clientX
  77. this.startY = e.changedTouches[0].clientY
  78. },
  79. handleTouchMoveOverflowPart(e : UniTouchEvent) {
  80. console.log("touchmove:" + e.touches[0].clientX + "," + e.touches[0].clientY);
  81. e.preventDefault()
  82. e.stopPropagation()
  83. const difX = e.changedTouches[0].clientX
  84. const difY = e.changedTouches[0].clientY
  85. this.moveX = difX - this.startX + this.oldX
  86. this.moveY = difY - this.startY + this.oldY
  87. this.moveEl?.style?.setProperty('transform', `translate(${this.moveX}px,${this.moveY}px)`)
  88. },
  89. handleTouchEndOverflowPart(_ : UniTouchEvent) {
  90. this.oldX = this.moveX
  91. this.oldY = this.moveY
  92. },
  93. handleClick(str : string) {
  94. console.log(`点击了 ${str} view`);
  95. if (str == 'red') {
  96. this.jest_result = true;
  97. }
  98. },
  99. async jest_getRect() {
  100. const rect = await uni.getElementById('child')!.getBoundingClientRectAsync()!
  101. let ratio = 1
  102. if (uni.getSystemInfoSync().platform == 'android'){
  103. ratio = uni.getSystemInfoSync().devicePixelRatio
  104. }
  105. this.jest_click_x = rect.x * ratio + 30
  106. this.jest_click_y = rect.bottom * ratio - 30
  107. },
  108. jest_getParentRect() {
  109. const transform = uni.getElementById('parent')?.style.getPropertyValue("transform")
  110. if (transform != null) {
  111. let value = transform as string
  112. value = value.split(",")[1].slice(0, -3)
  113. let ratio = 1
  114. if (uni.getSystemInfoSync().platform == 'android'){
  115. ratio = uni.getSystemInfoSync().devicePixelRatio
  116. }
  117. this.jest_parent_top = Math.round((parseFloat(value) * ratio))
  118. }
  119. },
  120. async jest_getAbsoluteViewRect() {
  121. const rect = await uni.getElementById('absolute-view')!.getBoundingClientRectAsync()!
  122. const systemInfo = uni.getSystemInfoSync()
  123. const titleBarHeight = systemInfo.screenHeight - systemInfo.windowHeight
  124. let ratio = 1
  125. if (uni.getSystemInfoSync().platform == 'android'){
  126. ratio = uni.getSystemInfoSync().devicePixelRatio
  127. }
  128. this.jest_click_x = rect.x * ratio + 30
  129. this.jest_click_y = (rect.top + titleBarHeight) * ratio + 30
  130. },
  131. async jest_scrollToDeepOverflow() {
  132. const scrollView = uni.getElementById('scroll-view') as UniScrollViewElement
  133. const rect = await uni.getElementById('deep-overflow')!.getBoundingClientRectAsync()!
  134. scrollView.scrollTo(0, rect.top)
  135. setTimeout(() => {
  136. const systemInfo = uni.getSystemInfoSync()
  137. let ratio = 1
  138. if (uni.getSystemInfoSync().platform == 'android'){
  139. ratio = uni.getSystemInfoSync().devicePixelRatio
  140. }
  141. const titleBarHeight = systemInfo.screenHeight - systemInfo.windowHeight
  142. uni.getElementById('deep-overflow')!.getBoundingClientRectAsync()!.then((afterRect: DOMRect) => {
  143. this.jest_click_x = afterRect.x * ratio + 30
  144. this.jest_click_y = (afterRect.top + titleBarHeight) * ratio + 30
  145. })
  146. }, 200)
  147. }
  148. }
  149. }
  150. </script>
  151. <style>
  152. .backgroundview {
  153. width: 300px;
  154. height: 300px;
  155. margin-bottom: 20px;
  156. background-color: white;
  157. justify-content: center;
  158. align-items: center;
  159. }
  160. .box-visible-border-radius {
  161. width: 100px;
  162. height: 100px;
  163. border-radius: 20px;
  164. overflow: visible;
  165. background-color: green;
  166. }
  167. .hover-class {
  168. background-color: aqua;
  169. }
  170. </style>