get-node-info.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. title: 'createSelectorQuery',
  31. top: 0,
  32. left: '0px',
  33. info: []
  34. }
  35. },
  36. onReady() {
  37. this.getNodeInfo()
  38. },
  39. methods: {
  40. setPosition() {
  41. this.left = Math.random() * uni.upx2px(320) + 'px'
  42. this.top = Math.random() * uni.upx2px(320) + 'px'
  43. this.getNodeInfo()
  44. },
  45. getNodeInfo() {
  46. uni.createSelectorQuery().select('.target').boundingClientRect().exec((ret) => {
  47. const rect = ret[0]
  48. if (rect) {
  49. const sort = ['left','right','top','bottom','width','height']
  50. const info = []
  51. for (let i in sort) {
  52. let key = sort[i]
  53. info.push({
  54. 'key': key,
  55. 'val': rect[key]
  56. })
  57. }
  58. this.info = info
  59. }
  60. });
  61. }
  62. },
  63. }
  64. </script>
  65. <style>
  66. .movable {
  67. display: flex;
  68. justify-content: center;
  69. }
  70. .block {
  71. height: 400rpx;
  72. width: 400rpx;
  73. background-color: #FFFFFF;
  74. border: 1px solid #ccc;
  75. position: relative;
  76. margin: 0 auto;
  77. margin-bottom: 30rpx;
  78. }
  79. movable-area {
  80. height: 400rpx;
  81. width: 400rpx;
  82. position: relative;
  83. }
  84. .target {
  85. height: 80rpx;
  86. width: 80rpx;
  87. line-height: 80rpx;
  88. text-align: center;
  89. color: #FFFFFF;
  90. background-color: #4cd964;
  91. font-size: 28rpx;
  92. position: absolute;
  93. }
  94. .info {
  95. display: flex;
  96. flex-direction: column;
  97. justify-content: center;
  98. align-items: center;
  99. }
  100. .b {
  101. font-weight: bold;
  102. width: 150rpx;
  103. display: inline-block;
  104. }
  105. .span {
  106. width: 100rpx;
  107. display: inline-block;
  108. }
  109. </style>