get-node-info.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <view class="movable block">
  6. <!-- #ifndef MP-TOUTIAO -->
  7. <movable-area>
  8. <movable-view class="target" direction="all" @change="getNodeInfo">Drag</movable-view>
  9. </movable-area>
  10. <!-- #endif -->
  11. <!-- #ifdef MP-TOUTIAO -->
  12. <view class="target" @click="setPosition" :style="{top:top,left:left}">Click</view>
  13. <!-- #endif -->
  14. </view>
  15. <view class="movable">
  16. <view class="info">
  17. <view v-for="(item,index) in info" :key="index">
  18. <text class="b">{{item.key}}</text>
  19. <text class="span">{{item.val}}</text>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. title: 'createSelectorQuery',
  32. top: 0,
  33. left: '0px',
  34. info: []
  35. }
  36. },
  37. onReady() {
  38. this.getNodeInfo()
  39. },
  40. methods: {
  41. setPosition() {
  42. this.left = Math.random() * uni.upx2px(320) + 'px'
  43. this.top = Math.random() * uni.upx2px(320) + 'px'
  44. this.getNodeInfo()
  45. },
  46. getNodeInfo() {
  47. uni.createSelectorQuery().select('.target').boundingClientRect().exec((ret) => {
  48. const rect = ret[0]
  49. if (rect) {
  50. const sort = ['left','right','top','bottom','width','height']
  51. const info = []
  52. for (let i in sort) {
  53. let key = sort[i]
  54. info.push({
  55. 'key': key,
  56. 'val': rect[key]
  57. })
  58. }
  59. this.info = info
  60. }
  61. });
  62. }
  63. },
  64. }
  65. </script>
  66. <style>
  67. .movable {
  68. display: flex;
  69. justify-content: center;
  70. }
  71. .block {
  72. height: 400rpx;
  73. width: 400rpx;
  74. background-color: #FFFFFF;
  75. border: 1px solid #ccc;
  76. position: relative;
  77. margin: 0 auto;
  78. margin-bottom: 30rpx;
  79. }
  80. movable-area {
  81. height: 400rpx;
  82. width: 400rpx;
  83. position: relative;
  84. }
  85. .target {
  86. height: 80rpx;
  87. width: 80rpx;
  88. line-height: 80rpx;
  89. text-align: center;
  90. color: #FFFFFF;
  91. background-color: #4cd964;
  92. font-size: 28rpx;
  93. position: absolute;
  94. }
  95. .info {
  96. display: flex;
  97. flex-direction: column;
  98. justify-content: center;
  99. align-items: center;
  100. }
  101. .b {
  102. font-weight: bold;
  103. width: 150rpx;
  104. display: inline-block;
  105. }
  106. .span {
  107. width: 100rpx;
  108. display: inline-block;
  109. }
  110. </style>