overflow-visible-event.uvue 3.1 KB
Newer Older
1 2
<template>
  <view>
3
    <text style="font-size: 15px;">overflow=visible 父view(绿色),子view(红色),点击超出父view区域的部分也可触发事件。</text>
4
    <view class="backgroundview">
5 6 7 8
      <view id="parent" class="box-visible-border-radius">
        <view id="child" style="width: 50px; height: 150px; background-color: red;" @click="handleClickOverflowPart"
          @touchmove="handleTouchMoveOverflowPart" @touchstart="handleTouchStartOverflowPart"
          @touchend="handleTouchEndOverflowPart">
9 10 11 12 13 14 15 16 17 18 19 20
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        jest_result: false,
        jest_click_x: -1,
21 22 23 24 25 26 27 28 29
        jest_click_y: -1,
        jest_parent_top: -1.0,
        startX: 0,
        startY: 0,
        moveX: 0,
        moveY: 0,
        oldX: 0,
        oldY: 0,
        moveEl: null as UniElement | null
30 31
      }
    },
32 33 34
    onReady() {
      this.moveEl = uni.getElementById('parent')
    },
35 36 37
    methods: {
      handleClickOverflowPart() {
        console.log("click");
38 39 40 41 42 43
        this.jest_result = true;
        uni.showToast({ "title": "点击红色区域" })
      },
      handleTouchStartOverflowPart(e : UniTouchEvent) {
        this.startX = e.changedTouches[0].clientX
        this.startY = e.changedTouches[0].clientY
44
      },
45
      handleTouchMoveOverflowPart(e : UniTouchEvent) {
46
        console.log("touchmove:" + e.touches[0].clientX + "," + e.touches[0].clientY);
47 48 49 50 51 52 53 54 55 56 57
        e.preventDefault()
        e.stopPropagation()
        const difX = e.changedTouches[0].clientX
        const difY = e.changedTouches[0].clientY
        this.moveX = difX - this.startX + this.oldX
        this.moveY = difY - this.startY + this.oldY
        this.moveEl?.style?.setProperty('transform', `translate(${this.moveX}px,${this.moveY}px)`)
      },
      handleTouchEndOverflowPart(_ : UniTouchEvent) {
        this.oldX = this.moveX
        this.oldY = this.moveY
58 59 60
      },
      jest_getRect() {
        const rect = uni.getElementById('child')?.getBoundingClientRect()
61
        if (rect != null) {
62 63 64
          const ratio = uni.getSystemInfoSync().devicePixelRatio
          this.jest_click_x = rect.x * ratio + 10
          this.jest_click_y = rect.bottom * ratio - 10
65 66 67 68 69 70 71 72 73 74
        }
      },
      jest_getParentRect() {
        const transform = uni.getElementById('parent')?.style.getPropertyValue("transform")
        if (transform != null) {
          let value = transform as string
          value = value.split(",")[1].slice(0, -3)
          const ratio = uni.getSystemInfoSync().devicePixelRatio
          this.jest_parent_top = Math.round((parseFloat(value) * ratio))
        }
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      }
    }
  }
</script>

<style>
  .backgroundview {
    width: 300px;
    height: 300px;
    margin-bottom: 20px;
    background-color: white;
    justify-content: center;
    align-items: center;
  }

  .box-visible-border-radius {
    width: 100px;
    height: 100px;
    border-radius: 20px;
    overflow: visible;
    background-color: green;
  }
</style>