touch-events-bubbles.uvue 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<template>
  <scroll-view style="flex: 1">
    <page-head title="事件冒泡测试"></page-head>
    <view class="container">
      <view class="view-box" id="view1" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
        <view class="mid-view" id="view1-2" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
          <image class="icon" id="view1-3" src="../image/logo.png" @touchstart="onTouchStart" @touchmove="onTouchMove"
            @touchend="onTouchEnd"></image>
        </view>
      </view>
      <view class="view-box" id="view2" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
        <view class="mid-view" id="view2-2" @touchend="onTouchEnd">
          <view style="background-color: beige;" class="icon" id="view2-3" @touchstart="onTouchStart" @touchend="onTouchEnd"></view>
        </view>
      </view>

    </view>
  </scroll-view>
</template>

<script>
  export default {
    data() {
      return {
        iconRect: null as DOMRect | null,
        viewEleRect: null as DOMRect | null,
27 28 29
        touchstartValue: [] as string[],
        touchmoveValue: [] as string[],
        touchendValue: [] as string[],
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        ret1: false,
        ret2: false
      }
    },
    onReady() {
      // #ifdef APP-IOS
      let iconEle = uni.getElementById("view1-3")
      this.iconRect = iconEle?.getBoundingClientRect()
      // 加上导航栏及状态栏高度
      this.iconRect.y += uni.getSystemInfoSync().safeArea.top + 44

      let viewEle = uni.getElementById("view2-3")
      this.viewEleRect = viewEle?.getBoundingClientRect()
      // 加上导航栏及状态栏高度
      this.viewEleRect.y += uni.getSystemInfoSync().safeArea.top + 44

      // #endif
    },
    methods: {
      clearValue() {
        this.touchstartValue = []
        this.touchmoveValue = []
        this.touchendValue = []
      },
      isPassTest1() {
        let result = this.touchstartValue.join("") == "view1-3view1-2view1" &&
          this.touchmoveValue.join("") == "view1-3view1-2view1" &&
          this.touchendValue.join("") == "view1-3view1-2view1"
        console.log('isPassTest1', result)
        this.ret1 = result
        this.clearValue()
      },
      isPassTest2() {
        let result = this.touchstartValue.join("") == "view2-3view2" &&
          this.touchmoveValue.join("") == "view2" &&
          this.touchendValue.join("") == "view2-3view2-2view2"
        console.log('isPassTest2', result)
        this.ret2 = result
        this.clearValue()
      },
      onTouchStart(e : TouchEvent) {
71
        let _id = e.currentTarget!.attributes.get("id") as string
72 73 74 75 76 77 78 79 80 81 82


        if (!this.touchstartValue.includes(_id)) {
          this.touchstartValue.push(_id)
        }

        console.log('onTouchStart', e.currentTarget!.attributes.get("id"))
      },
      onTouchMove(e : TouchEvent) {


83
        let _id = e.currentTarget!.attributes.get("id") as string
84 85 86 87 88 89 90 91 92

        if (!this.touchmoveValue.includes(_id)) {
          this.touchmoveValue.push(_id)
        }

        console.log('onTouchMove', e.currentTarget!.attributes.get("id"))
      },
      onTouchEnd(e : TouchEvent) {

93
        let _id = e.currentTarget!.attributes.get("id") as string
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

        if (!this.touchendValue.includes(_id)) {
          this.touchendValue.push(_id)
        }
      }
    }
  }
</script>

<style>
  .container {
    width: 100%;
    height: 80%;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
  }

  .view-box {
    width: 200px;
    height: 200px;
    align-items: center;
    justify-content: center;
    border-style: solid;
  }

  .mid-view {
    width: 150px;
    height: 150px;
    align-items: center;
    justify-content: center;
    background-color: aqua;
  }

  .icon {
    width: 100px;
    height: 100px;
  }
</style>