touch-events.uvue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <scroll-view style="flex: 1">
  3. <page-head title="拖拽图标测试相关事件(在小程序上本示例会卡顿,小程序上应使用movable-view)"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt" style="bottom: 20px;">
  5. <navigator url="/pages/component/global-events/touch-events-case" hover-class="none">
  6. <button type="default">
  7. stopPropagation / preventDefault
  8. </button>
  9. </navigator>
  10. </view>
  11. <view class="container">
  12. <view class="view-box" @touchstart="onViewTouchStart">
  13. <image class="icon" id="icon" src="../image/logo.png" @touchstart="onTouchStart" @touchcancel="onTouchCancel"
  14. @touchmove="onTouchMove" @touchend="onTouchEnd"></image>
  15. </view>
  16. </view>
  17. <view v-if="touchEvent !== null">
  18. <text class="title1">touches: </text>
  19. <template v-for="(touch, index) in touchEvent!.touches" :key="index">
  20. <text class="title2">touch[{{ index }}]:</text>
  21. <text>identifier: {{touch.identifier}}</text>
  22. <text>pageX: {{ touch.pageX }}, pageY: {{ touch.pageY }}</text>
  23. <text>clientX: {{ touch.clientX }}, clientY: {{ touch.clientY }}</text>
  24. <text>screenX: {{ touch.screenX }}, screenY: {{ touch.screenY }}</text>
  25. </template>
  26. </view>
  27. </scroll-view>
  28. </template>
  29. <script>
  30. export default {
  31. data() {
  32. return {
  33. move: false,
  34. posX: 0,
  35. posY: 0,
  36. lastX: 0,
  37. lastY: 0,
  38. touchEvent: null as TouchEvent | null,
  39. icon: null as UniElement | null,
  40. touchTargets: "",
  41. touchTargetsCount: 0,
  42. iconRect : null as DOMRect | null
  43. }
  44. },
  45. onReady() {
  46. this.icon = uni.getElementById("icon")
  47. // #ifdef APP-IOS || APP-HARMONY
  48. this.iconRect = this.icon?.getBoundingClientRect()
  49. // 加上导航栏及状态栏高度
  50. this.iconRect.y += uni.getSystemInfoSync().safeArea.top + 44
  51. // #endif
  52. },
  53. methods: {
  54. onViewTouchStart(e : TouchEvent) {
  55. this.touchTargets += e.target!.tagName + e.currentTarget!.tagName
  56. this.touchTargetsCount++
  57. },
  58. onTouchStart(e : TouchEvent) {
  59. this.touchTargetsCount++
  60. this.touchTargets += e.target!.tagName + e.currentTarget!.tagName
  61. this.touchEvent = e
  62. if (!this.move) {
  63. this.move = true
  64. this.posX = e.touches[0].screenX
  65. this.posY = e.touches[0].screenY
  66. }
  67. },
  68. onTouchMove(e : TouchEvent) {
  69. e.preventDefault()
  70. this.touchEvent = e
  71. let p = e.touches[0]
  72. if (p.screenX == this.lastX && p.screenY == this.lastY) {
  73. return
  74. }
  75. let x = p.screenX - this.posX
  76. let y = p.screenY - this.posY
  77. this.lastX = p.screenX
  78. this.lastY = p.screenY
  79. this.icon?.style?.setProperty('transform', 'translate(' + x + 'px,' + y + 'px)')
  80. },
  81. onTouchEnd(e : TouchEvent) {
  82. if (e.touches.length == 0) {
  83. this.resetIcon()
  84. this.touchEvent = null
  85. }
  86. },
  87. onTouchCancel(_ : TouchEvent) {
  88. this.resetIcon()
  89. this.touchEvent = null
  90. },
  91. resetIcon() {
  92. this.move = false;
  93. this.posX = 0;
  94. this.posY = 0;
  95. this.icon?.style?.setProperty('transform', 'translate(0px,0px)')
  96. }
  97. }
  98. }
  99. </script>
  100. <style>
  101. .container {
  102. width: 100%;
  103. flex-direction: column;
  104. align-items: center;
  105. }
  106. .view-box {
  107. width: 300px;
  108. height: 300px;
  109. align-items: center;
  110. justify-content: center;
  111. border-style: solid;
  112. }
  113. .icon {
  114. width: 100px;
  115. height: 100px;
  116. }
  117. .title1 {
  118. margin-top: 10px;
  119. font-size: 18px;
  120. }
  121. .title2 {
  122. margin-top: 5px;
  123. font-size: 16px;
  124. }
  125. </style>