title: NodesRef

sidebar_label: NodesRef

The object for obtaining the WXML node information.

Reference

Methods

boundingClientRect

Adds the request for querying the node layout position (in pixels) relative to the display area. This feature is similar to getBoundingClientRect of DOM. It returns SelectorQuery of NodesRef.

Reference

  1. (callback?: BoundingClientRectCallback) => SelectorQuery
Property Type Description
callback BoundingClientRectCallback The callback function. After the SelectorQuery.exec method is executed, the node information will be returned incallback.

Sample Code

Example 1
  1. Taro.createSelectorQuery().select('#the-id').boundingClientRect(function(rect){
  2. rect.id // The ID of the node
  3. rect.dataset // The dataset of the node
  4. rect.left // The left boundary coordinate of the node
  5. rect.right // The right boundary coordinate of the node
  6. rect.top // The upper boundary coordinate of the node
  7. rect.bottom // The lower boundary coordinate of the node
  8. rect.width // The width of the node
  9. rect.height // The height of the node
  10. }).exec()
Example 2
  1. Taro.createSelectorQuery().selectAll('.a-class').boundingClientRect(function(rects){
  2. rects.forEach(function(rect){
  3. rect.id // The ID of the node
  4. rect.dataset // The dataset of the node
  5. rect.left // The left boundary coordinate of the node
  6. rect.right // The right boundary coordinate of the node
  7. rect.top // The upper boundary coordinate of the node
  8. rect.bottom // The lower boundary coordinate of the node
  9. rect.width // The width of the node
  10. rect.height // The height of the node
  11. })
  12. }).exec()

API Support

API WeChat Mini-Program H5 React Native
NodesRef.boundingClientRect ✔️ ✔️

context

Adds the request for querying the node Context object. VideoContext, CanvasContext, LivePlayerContext, and MapContext can be obtained.

Reference

  1. (callback?: ContextCallback) => SelectorQuery
Property Type Description
callback ContextCallback The callback function. After the SelectorQuery.exec method is executed, the node information will be returned.

Sample Code

  1. Taro.createSelectorQuery().select('.the-video-class').context(function (res) {
  2. console.log(res.context) // The Context object of the node. If the selected node is a <video> component, the VideoContext object is returned.
  3. }).exec()

API Support

API WeChat Mini-Program H5 React Native
NodesRef.context ✔️

fields

Obtains the information about the node. The fields to be obtained are specified in fields. The selectorQuery of nodesRef is returned.

Note computedStyle has a higher priority than size. When width/height is specified and “size: true” is passed in computedStyle, the width/height obtained by computedStyle is returned first.

Reference

  1. (fields: Fields, callback?: FieldsCallback) => SelectorQuery
Property Type Description
fields Fields
callback FieldsCallback Callback function

Sample Code

  1. Taro.createSelectorQuery().select('#the-id').fields({
  2. dataset: true,
  3. size: true,
  4. scrollOffset: true,
  5. properties: ['scrollX', 'scrollY'],
  6. computedStyle: ['margin', 'backgroundColor'],
  7. context: true,
  8. }, function (res) {
  9. res.dataset // The dataset of the node
  10. res.width // The width of the node
  11. res.height // The height of the node
  12. res.scrollLeft // The horizontal scroll position of the node
  13. res.scrollTop // The vertical scroll position of the node
  14. res.scrollX // The current value of the node's scroll-x property
  15. res.scrollY // The current value of the node's scroll-y property
  16. // Return the specified style name
  17. res.margin
  18. res.backgroundColor
  19. res.context // The Context object of the node
  20. }).exec()

API Support

API WeChat Mini-Program H5 React Native
NodesRef.fields ✔️ ✔️

node

Gets the Node node instance. Currently Canvas is supported for fetching.

Reference

  1. (callback?: NodeCallback) => SelectorQuery
Property Type Description
callback NodeCallback The callback function. After the SelectorQuery.exec method is executed, the node information will be returned in callback.

Sample Code

  1. Taro.createSelectorQuery().select('.canvas').node(function(res){
  2. console.log(res.node)
  3. }).exec()

API Support

API WeChat Mini-Program H5 React Native
NodesRef.node ✔️

scrollOffset

Adds the request for querying the node scroll position (in pixels). The node must be scroll-view or viewport. It returns SelectorQuery of NodesRef.

Reference

  1. (callback?: ScrollOffsetCallback) => SelectorQuery
Property Type Description
callback ScrollOffsetCallback The callback function. After the SelectorQuery.exec method is executed, the node information will be returned in callback.

Sample Code

  1. Taro.createSelectorQuery().selectViewport().scrollOffset(function(res){
  2. res.id // The id of the node
  3. res.dataset // The dataset of the node
  4. res.scrollLeft // The horizontal scroll position of the node
  5. res.scrollTop // The vertical scroll position of the node
  6. }).exec()

API Support

API WeChat Mini-Program H5 React Native
NodesRef.scrollOffset ✔️ ✔️

Parameters

BoundingClientRectCallback

The callback function. After the SelectorQuery.exec method is executed, the node information will be returned in callback.

  1. (result: BoundingClientRectCallbackResult) => void
Property Type
result BoundingClientRectCallbackResult

BoundingClientRectCallbackResult

Property Type Description
dataset Record<string, any> The dataset of the node
id string The ID of the node
bottom number The lower boundary coordinate of the node
left number The left boundary coordinate of the node
right number The right boundary coordinate of the node
top number The upper boundary coordinate of the node
height number The height of the node
width number The width of the node

ContextCallback

The callback function. After the SelectorQuery.exec method is executed, the node information will be returned.

  1. (result: ContextCallbackResult) => void
Property Type
result ContextCallbackResult

ContextCallbackResult

Property Type Description
context Record<string, any> The Context object of the node

Fields

Property Type Required Description
computedStyle string[] No Specifies the style name list and returns the current value of the node style name
context boolean No Indicates whether to return the Context object of the node
dataset boolean No Indicates whether to return the node dataset
id boolean No Indicates whether to return the node ID
mark boolean No Specifies whether to return the node mark
node boolean No Specifies whether to return the Node instance corresponding to the node.
properties string[] No Specifies the property name list and returns the current property value of the node property name (only the general property values listed in the component document can be obtained, and “id class style” and the property values bound to events cannot be obtained)
rect boolean No Indicates whether to return the node layout position (left right top bottom)
scrollOffset boolean No Indicates whether to return the node’s scrollLeft and scrollTop. The node must be scroll-view or viewport.
size boolean No Indicates whether to return the node size (width and height)

FieldsCallback

  1. (res: Record<string, any>) => void
Property Type Description
res Record<string, any> Information about nodes

NodeCallback

The callback function. After the SelectorQuery.exec method is executed, the node information will be returned.

  1. (result: NodeCallbackResult) => void
Property Type
result NodeCallbackResult

NodeCallbackResult

Property Type Description
node Record<string, any> Node instances corresponding to nodes

ScrollOffsetCallback

The callback function. After the SelectorQuery.exec method is executed, the node information will be returned in callback.

  1. (result: ScrollOffsetCallbackResult) => void
Property Type
result ScrollOffsetCallbackResult

ScrollOffsetCallbackResult

Property Type Description
dataset Record<string, any> The dataset of the node
id string The ID of the node
scrollLeft number The horizontal scroll position of the node
scrollTop number The vertical scroll position of the node

API Support

API WeChat Mini-Program H5 React Native
NodesRef.boundingClientRect ✔️ ✔️
NodesRef.context ✔️
NodesRef.fields ✔️ ✔️
NodesRef.node ✔️
NodesRef.scrollOffset ✔️ ✔️