element-get-attribute.uvue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <view style="padding: 15px;">
  3. <view id="box" ref="box">
  4. <text class="uni-title-text">元素的id:{{ attrId }}</text>
  5. <!-- #ifndef APP -->
  6. <text class="uni-title-text">元素的style:{{ attrStyle }}</text>
  7. <!-- #endif -->
  8. <text class="uni-title-text">元素的背景色样式值:{{ propertyValue }}</text>
  9. <text class="uni-subtitle-text">小程序端:getAttribute 获取元素的属性值,目前仅支持id、style</text>
  10. <text class="uni-subtitle-text">App端:getAttribute 不支持获取 class、style 属性</text>
  11. </view>
  12. <button @click="getAttributeId">getAttribute获取元素的id</button>
  13. <button @click="setStyle">setProperty设置背景色</button>
  14. <!-- #ifndef APP -->
  15. <button @click="getAttributeStyle">getAttribute获取元素的style</button>
  16. <!-- #endif -->
  17. <button @click="getPropertyValue">getPropertyValue获取背景色值</button>
  18. <child id="child" ref="child"></child>
  19. <button @click="getBoundingClientRectAsyncChild">获取自定义组件child元素信息</button>
  20. <view class="rect-info" v-if="rectInfo">
  21. <view class="node-info-item">
  22. <text class="node-info-item-k">x: </text>
  23. <text class="node-info-item-v">{{rectInfo.x}}</text>
  24. </view>
  25. <view class="node-info-item">
  26. <text class="node-info-item-k">y: </text>
  27. <text class="node-info-item-v">{{rectInfo.y}}</text>
  28. </view>
  29. <view class="node-info-item">
  30. <text class="node-info-item-k">width: </text>
  31. <text class="node-info-item-v">{{rectInfo.width}}</text>
  32. </view>
  33. <view class="node-info-item">
  34. <text class="node-info-item-k">height: </text>
  35. <text class="node-info-item-v">{{rectInfo.height}}</text>
  36. </view>
  37. <view class="node-info-item">
  38. <text class="node-info-item-k">left: </text>
  39. <text class="node-info-item-v">{{rectInfo.left}}</text>
  40. </view>
  41. <view class="node-info-item">
  42. <text class="node-info-item-k">top: </text>
  43. <text class="node-info-item-v">{{rectInfo.top}}</text>
  44. </view>
  45. <view class="node-info-item">
  46. <text class="node-info-item-k">right: </text>
  47. <text class="node-info-item-v">{{rectInfo.right}}</text>
  48. </view>
  49. <view class="node-info-item">
  50. <text class="node-info-item-k">bottom: </text>
  51. <text class="node-info-item-v">{{rectInfo.bottom}}</text>
  52. </view>
  53. </view>
  54. <scroll-view ref="scrollView" class="scroll-view_H" direction="horizontal">
  55. <view class="scroll-view-item_H uni-bg-red"><text class="text">A</text></view>
  56. <view class="scroll-view-item_H uni-bg-green"><text class="text">B</text></view>
  57. <view class="scroll-view-item_H uni-bg-blue"><text class="text">C</text></view>
  58. </scroll-view>
  59. <!-- #ifndef WEB -->
  60. <button @click="scrollTo">scrollTo设置left滚动200px</button>
  61. <!-- #endif -->
  62. </view>
  63. </template>
  64. <script lang="uts">
  65. import child from './child.uvue'
  66. type DomRectType = {
  67. x : number,
  68. y : number,
  69. left : number,
  70. top : number,
  71. right : number,
  72. bottom : number,
  73. width : number,
  74. height : number,
  75. }
  76. export default {
  77. components: {
  78. child
  79. },
  80. data() {
  81. return {
  82. boxTarget: null as null | UniElement,
  83. scrollViewTarget: null as null | UniElement,
  84. attrId: "",
  85. attrStyle:"",
  86. propertyValue:"",
  87. rectInfo: {
  88. x: 0,
  89. y: 0,
  90. width: 0,
  91. height: 0,
  92. left: 0,
  93. top: 0,
  94. right: 0,
  95. bottom: 0,
  96. } as DomRectType,
  97. }
  98. },
  99. onReady() {
  100. this.boxTarget = this.$refs['box'] as UniElement
  101. this.scrollViewTarget = this.$refs['scrollView'] as UniElement;
  102. },
  103. methods: {
  104. getBoundingClientRectAsyncChild(){
  105. const childEl = uni.getElementById('child')!
  106. childEl.getBoundingClientRectAsync()!.then((rect : DOMRect) => {
  107. console.log('rect: ',rect);
  108. this.rectInfo = {
  109. x: rect.x,
  110. y: rect.y,
  111. width: rect.width,
  112. height: rect.height,
  113. left: rect.left,
  114. top: rect.top,
  115. right: rect.right,
  116. bottom: rect.bottom,
  117. } as DomRectType;;
  118. })
  119. },
  120. getAttributeId() {
  121. this.attrId = this.boxTarget!.getAttribute('id') ?? ""
  122. },
  123. setStyle() {
  124. this.boxTarget!.style.setProperty("background-color", "#FFF000")
  125. },
  126. getPropertyValue() {
  127. this.propertyValue = this.boxTarget!.style.getPropertyValue("background-color")
  128. },
  129. getAttributeStyle() {
  130. this.attrStyle = this.boxTarget!.getAttribute('style')?? ""
  131. },
  132. scrollTo() {
  133. // #ifdef MP-WEIXIN
  134. this.scrollViewTarget!.scrollTo({
  135. left: 200
  136. })
  137. // #endif
  138. // #ifndef MP-WEIXIN
  139. this.scrollViewTarget!.scrollTo(200,0)
  140. // #endif
  141. }
  142. }
  143. }
  144. </script>
  145. <style>
  146. .scroll-view_H {
  147. width: 100%;
  148. flex-direction: row;
  149. margin-top:15px;
  150. }
  151. .scroll-view-item_H {
  152. width: 100%;
  153. height: 150px;
  154. justify-content: center;
  155. align-items: center;
  156. }
  157. .text {
  158. font-size: 18px;
  159. color: #ffffff;
  160. }
  161. .rect-info {
  162. margin-top: 15px;
  163. padding: 10px;
  164. flex-direction: column;
  165. }
  166. .node-info-item {
  167. flex-direction: row;
  168. }
  169. .node-info-item-k {
  170. width: 72px;
  171. line-height: 2;
  172. }
  173. .node-info-item-v {
  174. font-weight: bold;
  175. line-height: 2;
  176. }
  177. </style>