resize-observer.uvue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view>
  3. <text>点击蓝色或红色方块区域会修改元素宽高</text>
  4. <view v-show="boxDisplay" style="align-items: center; justify-content: center; margin: 10px;">
  5. <view
  6. style="width: 140px; height: 140px; background-color: blue; align-items: center; justify-content: center; padding: 5px;"
  7. id="outBox" @click="outBoxClick">
  8. <view style="width: 80px; height: 80px; background-color: red; padding: 5px;" id="innerBox"
  9. @click="innerBoxClick"></view>
  10. </view>
  11. </view>
  12. <button @click="revertBoxSize">还原修改前元素宽高</button>
  13. <button @click="toggleDisplay">{{boxDisplay? '隐藏元素': '显示元素'}}</button>
  14. <button @click='cancelListen'>停止监听</button>
  15. <button @click='goOnListen'>恢复监听</button>
  16. <view>
  17. <text class="info-text">蓝色方块元素:</text>
  18. <view class="info-item">
  19. <text class="info-text">{{outBoxSizeInfo}}</text>
  20. </view>
  21. <text class="info-text" style="margin-top: 20px;">红色方块元素:</text>
  22. <view class="info-item">
  23. <text class="info-text">{{innerBoxSizeInfo}}</text>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. return {
  32. outBoxSizeInfo: "",
  33. innerBoxSizeInfo: "",
  34. offset: 2,
  35. boxDisplay: false,
  36. outBoxElement: null as UniElement | null,
  37. innerBoxElement: null as UniElement | null,
  38. resizeObserver: null as UniResizeObserver | null,
  39. outBoxElementOnResize: false
  40. }
  41. },
  42. onBackPress() : boolean {
  43. if (this.resizeObserver != null) {
  44. this.resizeObserver!.disconnect()
  45. }
  46. return false
  47. },
  48. onReady() {
  49. if (this.resizeObserver == null) {
  50. this.resizeObserver = new UniResizeObserver((entries : Array<UniResizeObserverEntry>) => {
  51. entries.forEach(entry => {
  52. if (entry.target == this.outBoxElement) {
  53. this.outBoxSizeInfo = this.analysisResizeObserverEntry(entry)
  54. this.outBoxElementOnResize = true
  55. } else if (entry.target == this.innerBoxElement) {
  56. this.innerBoxSizeInfo = this.analysisResizeObserverEntry(entry)
  57. }
  58. })
  59. })
  60. this.outBoxElement = uni.getElementById("outBox")
  61. if (this.outBoxElement != null) {
  62. this.resizeObserver!.observe(this.outBoxElement!)
  63. }
  64. this.innerBoxElement = uni.getElementById("innerBox")
  65. if (this.innerBoxElement != null) {
  66. this.resizeObserver!.observe(this.innerBoxElement!)
  67. }
  68. this.boxDisplay = true
  69. }
  70. },
  71. methods: {
  72. innerBoxClick() {
  73. if (this.innerBoxElement != null) {
  74. this.innerBoxElement!.style.setProperty("width", this.innerBoxElement!.offsetWidth + this.offset + 'px')
  75. this.innerBoxElement!.style.setProperty("height", this.innerBoxElement!.offsetWidth + this.offset + 'px')
  76. }
  77. },
  78. outBoxClick() {
  79. if (this.outBoxElement != null) {
  80. this.outBoxElement!.style.setProperty("width", this.outBoxElement!.offsetWidth + this.offset + 'px')
  81. this.outBoxElement!.style.setProperty("height", this.outBoxElement!.offsetWidth + this.offset + 'px')
  82. }
  83. },
  84. revertBoxSize() {
  85. if (this.outBoxElement != null) {
  86. this.outBoxElement!.style.setProperty("width", "140px")
  87. this.outBoxElement!.style.setProperty("height", "140px")
  88. }
  89. if (this.innerBoxElement != null) {
  90. this.innerBoxElement!.style.setProperty("width", "80px")
  91. this.innerBoxElement!.style.setProperty("height", "80px")
  92. }
  93. },
  94. //自动化测试专用
  95. setOutBoxMarginLeft(value : string) {
  96. if (this.outBoxElement != null) {
  97. this.outBoxElementOnResize = false
  98. this.outBoxElement!.style.setProperty("margin-left", value)
  99. }
  100. },
  101. toggleDisplay() {
  102. this.boxDisplay = !this.boxDisplay
  103. },
  104. analysisResizeObserverEntry(entry : UniResizeObserverEntry) : string {
  105. const contentBoxSize = entry.contentBoxSize[0]
  106. const borderBoxSize = entry.borderBoxSize[0]
  107. const devicePixelContentBoxSize = entry.devicePixelContentBoxSize[0]
  108. return "borderBoxSize: \n{blockSize:" + borderBoxSize.blockSize + ", inlineSize:" + borderBoxSize.inlineSize + "}\n" +
  109. "contentBoxSize: \n{blockSize:" + contentBoxSize.blockSize + ", inlineSize:" + contentBoxSize.inlineSize + "}\n" +
  110. "devicePixelContentBoxSize: \n{blockSize:" + devicePixelContentBoxSize.blockSize + ", inlineSize:" + devicePixelContentBoxSize.inlineSize + "}\n" +
  111. "contentRect: \n{x:" + entry.contentRect.x + ", y:" + entry.contentRect.y + ", width:" + entry.contentRect.width + ", height:" + entry.contentRect.height + "}"
  112. },
  113. cancelListen() {
  114. // this.resizeObserver?.unobserve()
  115. this.resizeObserver!.unobserve(this.outBoxElement!)
  116. this.resizeObserver!.unobserve(this.innerBoxElement!)
  117. },
  118. goOnListen() {
  119. this.resizeObserver!.observe(this.outBoxElement!)
  120. this.resizeObserver!.observe(this.innerBoxElement!)
  121. }
  122. }
  123. }
  124. </script>
  125. <style>
  126. .info-item {
  127. flex-direction: row;
  128. }
  129. .info-text {
  130. font-size: 14px;
  131. }
  132. </style>