overflow-visible-event.uvue 6.7 KB
Newer Older
1
<template>
2
  <!-- #ifdef APP -->
3
  <scroll-view id="scroll-view" style="flex: 1;">
DCloud-WZF's avatar
DCloud-WZF 已提交
4
  <!-- #endif -->
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
5
    <view>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
      <text style="font-size: 15px;">overflow=visible 父view(绿色),子view(红色),点击超出父view区域的部分也可触发事件。</text>
      <view class="backgroundview">
        <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">
          </view>
        </view>
      </view>
      <text style="font-size: 15px;">overflow=visible组件的子view (绿色)被position:
        absolute的view(红色)遮挡时,点击事件测试。</text>
      <view style="width: 300px;height: 300px;background-color: white;">
        <view style="overflow:visible;height: 100px;width: 100%;">
          <view style="background-color: green;height: 100px;width: 100%;" @click="handleClick('green')">
          </view>
        </view>
        <view id="absolute-view" style="position: absolute;background-color: red;width: 100px;height: 200px;right: 0px;"
          @click="handleClick('red')">
        </view>
      </view>
      <text style="font-size: 15px;">overflow=visible组件设置hover-class的测试</text>
      <view class="backgroundview">
        <view class="box-visible-border-radius" hover-class="hover-class">
          <view style="background-color: red;height: 100px;width: 100px;margin-left: 20px;margin-top: 20px;">
          </view>
31
        </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
32 33 34 35 36 37 38 39 40 41 42 43
      </view>
      <text style="font-size: 15px;">overflow=visible组件多层级设置测试</text>
      <view class="backgroundview">
        <view id="deep-overflow" style="overflow: visible;">
          <view style="overflow: visible;background-color: red;width: 100px;height: 100px;" @click="handleClick('red')">
          </view>
        </view>
        <view style="overflow: visible;">
          <view style="overflow: visible;background-color: green;width: 100px;height: 100px;">
          </view>
        </view>
      </view>
44
    </view>
45
  <!-- #ifdef APP -->
46
  </scroll-view>
47
  <!-- #endif -->
48 49 50 51 52 53 54 55
</template>

<script>
  export default {
    data() {
      return {
        jest_result: false,
        jest_click_x: -1,
56 57 58 59 60 61 62 63 64
        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
65 66
      }
    },
67 68 69
    onReady() {
      this.moveEl = uni.getElementById('parent')
    },
70 71 72
    methods: {
      handleClickOverflowPart() {
        console.log("click");
73 74 75 76 77 78
        this.jest_result = true;
        uni.showToast({ "title": "点击红色区域" })
      },
      handleTouchStartOverflowPart(e : UniTouchEvent) {
        this.startX = e.changedTouches[0].clientX
        this.startY = e.changedTouches[0].clientY
79
      },
80
      handleTouchMoveOverflowPart(e : UniTouchEvent) {
81
        console.log("touchmove:" + e.touches[0].clientX + "," + e.touches[0].clientY);
82 83 84 85 86 87 88 89 90 91 92
        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
93
      },
94 95 96 97 98 99
      handleClick(str : string) {
        console.log(`点击了 ${str} view`);
        if (str == 'red') {
          this.jest_result = true;
        }
      },
100 101
      async jest_getRect() {
        const rect = await uni.getElementById('child')!.getBoundingClientRectAsync()
102
        if (rect != null) {
103
          let ratio = 1
104 105 106
          if (uni.getSystemInfoSync().platform == 'android'){
            ratio = uni.getSystemInfoSync().devicePixelRatio
          }
107
          this.jest_click_x = rect.x * ratio + 30
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
108
          this.jest_click_y = rect.bottom * ratio - 30
109 110 111
        }
      },
      jest_getParentRect() {
112
        const transform = uni.getElementById('parent')?.style.getPropertyValue("transform")
113
        if (transform != null) {
114
          let value = transform as string
115 116
          value = value.split(",")[1].slice(0, -3)
          let ratio = 1
117 118 119
          if (uni.getSystemInfoSync().platform == 'android'){
            ratio = uni.getSystemInfoSync().devicePixelRatio
          }
120
          this.jest_parent_top = Math.round((parseFloat(value) * ratio))
121
        }
122
      },
123 124
      async jest_getAbsoluteViewRect() {
        const rect = await uni.getElementById('absolute-view')!.getBoundingClientRectAsync()
125 126
        if (rect != null) {
          const systemInfo = uni.getSystemInfoSync()
127 128
          const titleBarHeight = systemInfo.screenHeight - systemInfo.windowHeight
          let ratio = 1
129 130 131
          if (uni.getSystemInfoSync().platform == 'android'){
            ratio = uni.getSystemInfoSync().devicePixelRatio
          }
132
          this.jest_click_x = rect.x * ratio + 30
133
          this.jest_click_y = (rect.top + titleBarHeight) * ratio + 30
134
        }
DCloud-WZF's avatar
DCloud-WZF 已提交
135
      },
136
      async jest_scrollToDeepOverflow() {
DCloud-WZF's avatar
DCloud-WZF 已提交
137
        const scrollView = uni.getElementById('scroll-view') as UniScrollViewElement
138
        const rect = await uni.getElementById('deep-overflow')!.getBoundingClientRectAsync()
DCloud-WZF's avatar
DCloud-WZF 已提交
139 140 141
        if (rect != null) {
          scrollView.scrollTo(0, rect.top)
          setTimeout(() => {
142 143
            const systemInfo = uni.getSystemInfoSync()
            let ratio = 1
144 145 146
            if (uni.getSystemInfoSync().platform == 'android'){
              ratio = uni.getSystemInfoSync().devicePixelRatio
            }
DCloud-WZF's avatar
DCloud-WZF 已提交
147
            const titleBarHeight = systemInfo.screenHeight - systemInfo.windowHeight
148 149 150 151 152 153
            uni.getElementById('deep-overflow')!.getBoundingClientRectAsync().then((afterRect: DOMRect) => {
              if (afterRect != null) {
                this.jest_click_x = afterRect.x * ratio + 30
                this.jest_click_y = (afterRect.top + titleBarHeight) * ratio + 30
              }
            })
DCloud-WZF's avatar
DCloud-WZF 已提交
154 155
          }, 200)
        }
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
      }
    }
  }
</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;
  }
178

179 180
  .hover-class {
    background-color: aqua;
181
  }
182
</style>