nodes-info.uvue 4.6 KB
Newer Older
H
hdx 已提交
1
<template>
H
hdx 已提交
2
  <view class="page" id="page">
H
hdx 已提交
3
    <page-head :title="title"></page-head>
4 5
    <button class="btn btn-get-node-info" @click="getNodeInfo">getNodeInfo</button>
    <button class="btn btn-get-all-node-info" @click="getAllNodeInfo">getAllNodeInfo</button>
6
    <view id="rect-1-2" class="rect-1-2">
7 8
      <view class="rect rect1"></view>
      <view class="rect rect2"></view>
H
hdx 已提交
9
    </view>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    <view class="rect-info-1-2">
      <view class="rect-info" v-for="(nodeInfo, index) in nodeInfoList" :key="index">
        <view class="node-info-item">
          <text class="node-info-item-k">left: </text>
          <text class="node-info-item-v">{{nodeInfo.left}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">top: </text>
          <text class="node-info-item-v">{{nodeInfo.top}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">right: </text>
          <text class="node-info-item-v">{{nodeInfo.right}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">bottom: </text>
          <text class="node-info-item-v">{{nodeInfo.bottom}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">width: </text>
          <text class="node-info-item-v">{{nodeInfo.width}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">height: </text>
          <text class="node-info-item-v">{{nodeInfo.height}}</text>
        </view>
H
hdx 已提交
36 37 38 39 40 41
      </view>
    </view>
  </view>
</template>

<script>
42 43 44 45 46 47 48 49 50
  type NodeInfoType = {
    left : number | null,
    top : number | null,
    right : number | null,
    bottom : number | null,
    width : number | null,
    height : number | null,
  }

H
hdx 已提交
51 52 53 54
  export default {
    data() {
      return {
        title: 'createSelectorQuery',
55
        nodeInfoList: [] as NodeInfoType[],
H
hdx 已提交
56 57
        // 仅用于自动化测试
        rootNodeInfo: null as NodeInfoType | null,
58
        //供自动化测试使用
59
        // resizeRectValid: false
60 61 62 63
      }
    },
    onResize() {
      //供自动化测试使用
64
      /* var rect12Element = uni.getElementById("rect-1-2")
65 66 67 68 69
      if(rect12Element != null) {
        var domRect = rect12Element.getBoundingClientRect()
        if(domRect.width > 100) {
          this.resizeRectValid = true
        }
70
      } */
H
hdx 已提交
71 72
    },
    methods: {
H
hdx 已提交
73
      // 仅用于自动化测试
H
hdx 已提交
74 75
      getRootNodeInfo(selector: string) {
        uni.createSelectorQuery().select(selector).boundingClientRect().exec((ret) => {
H
hdx 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89
          if (ret.length == 1) {
            const nodeInfo = ret[0] as NodeInfo;
            const nodeType = {
              left: nodeInfo.left,
              top: nodeInfo.top,
              right: nodeInfo.right,
              bottom: nodeInfo.bottom,
              width: nodeInfo.width,
              height: nodeInfo.height,
            } as NodeInfoType;
            this.rootNodeInfo = nodeType
          }
        })
      },
H
hdx 已提交
90
      getNodeInfo() {
91 92
        uni.createSelectorQuery().select('.rect1').boundingClientRect().exec((ret) => {
          this.nodeInfoList.length = 0
H
hdx 已提交
93
          const i = ret[0] as NodeInfo
94 95 96 97 98 99 100 101
          this.nodeInfoList.push({
            left: i.left,
            top: i.top,
            right: i.right,
            bottom: i.bottom,
            width: i.width,
            height: i.height,
          } as NodeInfoType)
102 103 104 105 106
        })
      },
      getAllNodeInfo() {
        uni.createSelectorQuery().selectAll('.rect').boundingClientRect().exec((ret) => {
          this.nodeInfoList.length = 0
H
hdx 已提交
107
          const array = ret[0] as NodeInfo[]
108 109 110 111 112 113 114 115 116 117
          array.forEach((i) => {
            this.nodeInfoList.push({
              left: i.left,
              top: i.top,
              right: i.right,
              bottom: i.bottom,
              width: i.width,
              height: i.height,
            } as NodeInfoType)
          })
H
hdx 已提交
118 119 120 121 122 123 124
        })
      }
    }
  }
</script>

<style>
125 126
  .page {
    padding: 15px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
127
    flex: 1;
128 129
  }

130 131 132 133 134 135 136 137 138
  .btn {
    margin-top: 15px;
  }

  .rect-1-2 {
    flex-direction: row;
    margin-top: 15px;
  }

H
hdx 已提交
139
  .rect {
140 141 142 143 144
    width: 150px;
    height: 100px;
  }

  .rect1 {
H
hdx 已提交
145
    background-color: dodgerblue;
146 147 148 149 150 151 152 153 154 155
  }

  .rect2 {
    margin-left: auto;
    background-color: seagreen;
  }

  .rect-info-1-2 {
    flex-direction: row;
    margin-top: 15px;
H
hdx 已提交
156 157 158
  }

  .rect-info {
159
    flex: 1;
H
hdx 已提交
160 161 162 163 164 165 166 167
    flex-direction: column;
  }

  .node-info-item {
    flex-direction: row;
  }

  .node-info-item-k {
168 169
    width: 72px;
    line-height: 2;
H
hdx 已提交
170 171 172 173
  }

  .node-info-item-v {
    font-weight: bold;
174
    line-height: 2;
H
hdx 已提交
175
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
176
</style>