touch-events-bubbles.uvue 4.3 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
        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() {
55 56 57 58 59 60 61 62 63 64 65

        let touchStart = this.touchstartValue.join("")
        let touchMove = this.touchmoveValue.join("")
        let touchEnd = this.touchendValue.join("")
        console.log("touchStart: ", touchStart)
        console.log("touchMove: ", touchMove)
        console.log("touchEnd: ", touchEnd)

        let result = touchStart == "view1-3view1-2view1" &&
          touchMove == "view1-3view1-2view1" &&
          touchEnd == "view1-3view1-2view1"
66 67 68 69 70
        console.log('isPassTest1', result)
        this.ret1 = result
        this.clearValue()
      },
      isPassTest2() {
71 72 73 74 75 76 77 78 79 80 81

        let touchStart = this.touchstartValue.join("")
        let touchMove = this.touchmoveValue.join("")
        let touchEnd = this.touchendValue.join("")
        console.log("touchStart: ", touchStart)
        console.log("touchMove: ", touchMove)
        console.log("touchEnd: ", touchEnd)

        let result = touchStart == "view2-3view2" &&
          touchMove == "view2" &&
          touchEnd == "view2-3view2-2view2"
82 83 84 85 86
        console.log('isPassTest2', result)
        this.ret2 = result
        this.clearValue()
      },
      onTouchStart(e : TouchEvent) {
87
        let _id = e.currentTarget!.attributes.get("id") as string
88 89 90 91 92 93 94 95 96 97 98


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

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


99
        let _id = e.currentTarget!.attributes.get("id") as string
100 101 102 103 104 105 106 107 108

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

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

109
        let _id = e.currentTarget!.attributes.get("id") as string
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

        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>