element-get-bounding-client-rect-async.uvue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view class="page" id="page">
  6. <page-head :title="title"></page-head>
  7. <button class="btn btn-get-all-node-info" @click="getBoundingClientRectAsync">getBoundingClientRectAsync</button>
  8. <view id="rect-test" ref="rect-test" class="rect-test"></view>
  9. <view class="rect-info" v-if="rectInfo">
  10. <view class="node-info-item">
  11. <text class="node-info-item-k">x: </text>
  12. <text class="node-info-item-v">{{rectInfo.x}}</text>
  13. </view>
  14. <view class="node-info-item">
  15. <text class="node-info-item-k">y: </text>
  16. <text class="node-info-item-v">{{rectInfo.y}}</text>
  17. </view>
  18. <view class="node-info-item">
  19. <text class="node-info-item-k">width: </text>
  20. <text class="node-info-item-v">{{rectInfo.width}}</text>
  21. </view>
  22. <view class="node-info-item">
  23. <text class="node-info-item-k">height: </text>
  24. <text class="node-info-item-v">{{rectInfo.height}}</text>
  25. </view>
  26. <view class="node-info-item">
  27. <text class="node-info-item-k">left: </text>
  28. <text class="node-info-item-v">{{rectInfo.left}}</text>
  29. </view>
  30. <view class="node-info-item">
  31. <text class="node-info-item-k">top: </text>
  32. <text class="node-info-item-v">{{rectInfo.top}}</text>
  33. </view>
  34. <view class="node-info-item">
  35. <text class="node-info-item-k">right: </text>
  36. <text class="node-info-item-v">{{rectInfo.right}}</text>
  37. </view>
  38. <view class="node-info-item">
  39. <text class="node-info-item-k">bottom: </text>
  40. <text class="node-info-item-v">{{rectInfo.bottom}}</text>
  41. </view>
  42. </view>
  43. </view>
  44. <!-- #ifdef APP -->
  45. </scroll-view>
  46. <!-- #endif -->
  47. </template>
  48. <script>
  49. type DomRectType = {
  50. x : number,
  51. y : number,
  52. left : number,
  53. top : number,
  54. right : number,
  55. bottom : number,
  56. width : number,
  57. height : number,
  58. }
  59. export default {
  60. data() {
  61. return {
  62. title: 'getBoundingClientRectAsync',
  63. rectInfo: {
  64. x: 0,
  65. y: 0,
  66. width: 0,
  67. height: 0,
  68. left: 0,
  69. top: 0,
  70. right: 0,
  71. bottom: 0,
  72. } as DomRectType,
  73. }
  74. },
  75. methods: {
  76. getBoundingClientRectAsync() {
  77. (this.$refs["rect-test"] as UniElement).getBoundingClientRectAsync()!.then((rect : DOMRect) => {
  78. this.rectInfo = {
  79. x: rect.x,
  80. y: rect.y,
  81. width: rect.width,
  82. height: rect.height,
  83. left: rect.left,
  84. top: rect.top,
  85. right: rect.right,
  86. bottom: rect.bottom,
  87. } as DomRectType;;
  88. })
  89. }
  90. }
  91. }
  92. </script>
  93. <style>
  94. .page {
  95. padding: 15px;
  96. }
  97. .btn {
  98. margin-top: 15px;
  99. }
  100. .rect-test {
  101. margin-top: 15px;
  102. height: 100px;
  103. background-color: dodgerblue;
  104. }
  105. .rect-info {
  106. margin-top: 15px;
  107. flex-direction: column;
  108. }
  109. .node-info-item {
  110. flex-direction: row;
  111. }
  112. .node-info-item-k {
  113. width: 72px;
  114. line-height: 2;
  115. }
  116. .node-info-item-v {
  117. font-weight: bold;
  118. line-height: 2;
  119. }
  120. </style>