resize-observer.uvue 5.4 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2 3 4 5 6
<template>
  <view>
    <text>点击蓝色或红色方块区域会修改元素宽高</text>
    <view v-show="boxDisplay" style="align-items: center; justify-content: center; margin: 10px;">
      <view
        style="width: 140px; height: 140px; background-color: blue; align-items: center; justify-content: center; padding: 5px;"
辛宝Otto's avatar
辛宝Otto 已提交
7
        id="outBox" @click="outBoxClick">
shutao-dc's avatar
shutao-dc 已提交
8 9 10 11 12
        <view style="width: 80px; height: 80px; background-color: red; padding: 5px;" id="innerBox"
          @click="innerBoxClick"></view>
      </view>
    </view>
    <button @click="revertBoxSize">还原修改前元素宽高</button>
辛宝Otto's avatar
辛宝Otto 已提交
13 14 15
    <button @click="toggleDisplay">{{boxDisplay? '隐藏元素': '显示元素'}}</button>
    <button @click='cancelListen'>停止监听</button>
    <button @click='goOnListen'>恢复监听</button>
shutao-dc's avatar
shutao-dc 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    <view>
      <text class="info-text">蓝色方块元素:</text>
      <view class="info-item">
        <text class="info-text">{{outBoxSizeInfo}}</text>
      </view>
      <text class="info-text" style="margin-top: 20px;">红色方块元素:</text>
      <view class="info-item">
        <text class="info-text">{{innerBoxSizeInfo}}</text>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        outBoxSizeInfo: "",
        innerBoxSizeInfo: "",
        offset: 2,
shutao-dc's avatar
shutao-dc 已提交
36
        boxDisplay: false,
shutao-dc's avatar
shutao-dc 已提交
37 38
        outBoxElement: null as UniElement | null,
        innerBoxElement: null as UniElement | null,
辛宝Otto's avatar
辛宝Otto 已提交
39
        resizeObserver: null as UniResizeObserver | null,
40
        outBoxElementOnResize: false
shutao-dc's avatar
shutao-dc 已提交
41 42 43 44 45 46 47 48
      }
    },
    onBackPress() : boolean {
      if (this.resizeObserver != null) {
        this.resizeObserver!.disconnect()
      }
      return false
    },
shutao-dc's avatar
shutao-dc 已提交
49
    onReady() {
shutao-dc's avatar
shutao-dc 已提交
50 51
      if (this.resizeObserver == null) {
        this.resizeObserver = new UniResizeObserver((entries : Array<UniResizeObserverEntry>) => {
辛宝Otto's avatar
辛宝Otto 已提交
52

shutao-dc's avatar
shutao-dc 已提交
53 54
          entries.forEach(entry => {
            if (entry.target == this.outBoxElement) {
辛宝Otto's avatar
辛宝Otto 已提交
55
              this.outBoxSizeInfo = this.analysisResizeObserverEntry(entry)
56
              this.outBoxElementOnResize = true
shutao-dc's avatar
shutao-dc 已提交
57 58 59 60 61 62 63
            } else if (entry.target == this.innerBoxElement) {
              this.innerBoxSizeInfo = this.analysisResizeObserverEntry(entry)
            }
          })
        })
        this.outBoxElement = uni.getElementById("outBox")
        if (this.outBoxElement != null) {
shutao-dc's avatar
shutao-dc 已提交
64
          this.resizeObserver!.observe(this.outBoxElement!)
shutao-dc's avatar
shutao-dc 已提交
65 66 67
        }
        this.innerBoxElement = uni.getElementById("innerBox")
        if (this.innerBoxElement != null) {
shutao-dc's avatar
shutao-dc 已提交
68
          this.resizeObserver!.observe(this.innerBoxElement!)
辛宝Otto's avatar
辛宝Otto 已提交
69
        }
shutao-dc's avatar
shutao-dc 已提交
70
        this.boxDisplay = true
shutao-dc's avatar
shutao-dc 已提交
71 72 73 74 75
      }
    },
    methods: {
      innerBoxClick() {
        if (this.innerBoxElement != null) {
雪洛's avatar
雪洛 已提交
76 77
          this.innerBoxElement!.style.setProperty("width", this.innerBoxElement!.offsetWidth + this.offset + 'px')
          this.innerBoxElement!.style.setProperty("height", this.innerBoxElement!.offsetWidth + this.offset + 'px')
shutao-dc's avatar
shutao-dc 已提交
78 79 80 81
        }
      },
      outBoxClick() {
        if (this.outBoxElement != null) {
雪洛's avatar
雪洛 已提交
82 83
          this.outBoxElement!.style.setProperty("width", this.outBoxElement!.offsetWidth + this.offset + 'px')
          this.outBoxElement!.style.setProperty("height", this.outBoxElement!.offsetWidth + this.offset + 'px')
shutao-dc's avatar
shutao-dc 已提交
84 85 86 87 88 89 90 91 92 93 94
        }
      },
      revertBoxSize() {
        if (this.outBoxElement != null) {
          this.outBoxElement!.style.setProperty("width", "140px")
          this.outBoxElement!.style.setProperty("height", "140px")
        }
        if (this.innerBoxElement != null) {
          this.innerBoxElement!.style.setProperty("width", "80px")
          this.innerBoxElement!.style.setProperty("height", "80px")
        }
辛宝Otto's avatar
辛宝Otto 已提交
95 96 97 98 99 100 101
      },
      //自动化测试专用
      setOutBoxMarginLeft(value : string) {
        if (this.outBoxElement != null) {
          this.outBoxElementOnResize = false
          this.outBoxElement!.style.setProperty("margin-left", value)
        }
shutao-dc's avatar
shutao-dc 已提交
102 103 104 105 106 107 108 109 110 111 112 113
      },
      toggleDisplay() {
        this.boxDisplay = !this.boxDisplay
      },
      analysisResizeObserverEntry(entry : UniResizeObserverEntry) : string {
        const contentBoxSize = entry.contentBoxSize[0]
        const borderBoxSize = entry.borderBoxSize[0]
        const devicePixelContentBoxSize = entry.devicePixelContentBoxSize[0]
        return "borderBoxSize: \n{blockSize:" + borderBoxSize.blockSize + ", inlineSize:" + borderBoxSize.inlineSize + "}\n" +
          "contentBoxSize: \n{blockSize:" + contentBoxSize.blockSize + ", inlineSize:" + contentBoxSize.inlineSize + "}\n" +
          "devicePixelContentBoxSize: \n{blockSize:" + devicePixelContentBoxSize.blockSize + ", inlineSize:" + devicePixelContentBoxSize.inlineSize + "}\n" +
          "contentRect: \n{x:" + entry.contentRect.x + ", y:" + entry.contentRect.y + ", width:" + entry.contentRect.width + ", height:" + entry.contentRect.height + "}"
辛宝Otto's avatar
辛宝Otto 已提交
114 115 116 117 118 119 120 121 122
      },
      cancelListen(){
        // this.resizeObserver?.unobserve()
        this.resizeObserver!.unobserve(this.outBoxElement!)
        this.resizeObserver!.unobserve(this.innerBoxElement!)
      },
      goOnListen(){
        this.resizeObserver!.observe(this.outBoxElement!)
        this.resizeObserver!.observe(this.innerBoxElement!)
shutao-dc's avatar
shutao-dc 已提交
123 124
      }
    }
125
  }
shutao-dc's avatar
shutao-dc 已提交
126 127 128 129 130 131 132
</script>

<style>
  .info-item {
    flex-direction: row;
  }

133
  .info-text {
shutao-dc's avatar
shutao-dc 已提交
134 135
    font-size: 14px;
  }
136
</style>