The application of this API is through SelectorQuery wx.createSelectorQuery().
this.createSelectorQuery()
.const query = wx.createSelectorQuery()query.select('#the-id').boundingClientRect()query.selectViewport().scrollOffset()query.exec(function (res) {res[0].top // The upper boundary coordinate of the #the-id node.res[1].scrollTop // The vertical scroll position of the display area.})
The usage of this API is IntersectionObserver wx.createIntersectionObserver(Object component, Object options).
this.createIntersectionObserver([options])
should be used as a substitute.Attributes | Types | Default value | Required | Note |
thresholds | Array.<number> | [0] | Not required | An array of numerical values, encompassing all thresholds. |
initialRatio | number | 0 | Not required | The initial intersection ratio, if the detected intersection ratio at the time of invocation is not equivalent to this value and reaches the threshold, it will trigger a callback function of the listener once. |
observeAll | boolean | false | Not required | Whether to observe multiple target nodes simultaneously (rather than a single one). If set to true, the targetSelector of observe will select multiple nodes (be aware: selecting too many nodes at once may impact rendering performance). |
The method is utilized as follows: IntersectionObserver.relativeTo(string selector, Object margins)
Attributes | Types | Default value | Required | Note |
left | number | - | Not required | The left boundary of the node's layout area. |
right | number | - | Not required | The right boundary of the node's layout area. |
top | number | - | Not required | The upper boundary of the node's layout area. |
bottom | number | - | Not required | The lower boundary of the node's layout area. |
The method is utilized as IntersectionObserver.relativeToViewport(Object margins).
Attributes | Types | Default value | Required | Note |
left | number | - | Not required | The left boundary of the node's layout area. |
right | number | - | Not required | The right boundary of the node's layout area. |
top | number | - | Not required | The upper boundary of the node's layout area. |
bottom | number | - | Not required | The lower boundary of the node's layout area. |
Page({onLoad() {wx.createIntersectionObserver().relaonLoad: function(){wx.createIntersectionObserver().relativeToViewport({bottom: 100}).observe('.target-class', (res) => {res.intersectionRatio // the ratio of the intersection region to the layout region of the target noderes.intersectionRect // intersection regionres.intersectionRect.left // coordinates of the left boundary of the intersection regionres.intersectionRect.top // the upper boundary of the intersection regionres.intersectionRect.width // width of the intersection regionres.intersectionRect.height // height of the intersection region})}})
The method is utilized as IntersectionObserver.disconnect().
The method is utilized as IntersectionObserver.observe(string targetSelector, function callback).
Structural attributes | Types | Note |
intersectionRatio | number | Intersection Ratio |
intersectionRect | object | Boundary of the Intersection Area |
boundingClientRect | object | Target Boundary |
relativeRect | object | Boundary of the Reference Area |
time | number | Timestamp during Intersection Detection |
Structural attributes | Types | Note |
left | number | Left Boundary |
right | number | Right Boundary |
top | number | Upper Boundary |
bottom | number | Lower Boundary |
Structural attributes | Types | Note |
left | number | Left Boundary |
right | number | Right Boundary |
top | number | Upper Boundary |
bottom | number | Lower Boundary |
Structural attributes | Types | Note |
left | number | Left Boundary |
right | number | Right Boundary |
top | number | Upper Boundary |
bottom | number | Lower Boundary |
The method is utilized as NodesRef.fields(Object fields, function callback)
Attributes | Types | Default value | Required | Note |
id | boolean | false | Not required | Whether to return the node id |
dataset | boolean | false | Not required | Whether to return the node dataset |
rect | boolean | false | Not required | Whether to return the layout position of the node (left, right, top, bottom) |
size | boolean | false | Not required | Whether to return the dimensions of the node (width, height) |
scrollOffset | boolean | false | Not required | Whether to return the scrollLeft scrollTop of the node, the node must be a scroll-view or viewport |
properties | Array.<string> | - | Not required | Specify a list of attribute names to return the current attribute values of the corresponding node (only the regular attribute values marked in the component documentation can be obtained, the attribute values of id, class, style, and event binding cannot be retrieved) |
computedStyle | Array.<string> | - | Not required | Specify a list of style names to return the current values of the corresponding style names for the node |
context | boolean | false | Not required | Whether to return the corresponding Context object of the node |
Page({getFields () {wx.createSelectorQuery().select('#the-id').fields({dataset: true,size: true,scrollOffset: true,properties: ['scrollX', 'scrollY'],computedStyle: ['margin', 'backgroundColor'],context: true, }, function (res) { {}, function (res) {res.dataset // the node's datasetres.width // width of the noderes.height // the height of the noderes.scrollLeft // The horizontal scroll position of the node.res.scrollTop // The vertical scroll position of the node.res.scrollX // The current value of the node's scroll-x property.res.scrollY // The current value of the node's scroll-y property.// This returns the name of the style to be returned.res.marginres.backgroundColorres.context // The node's corresponding Context object.res.ref // The Ref object for the node.res.ref // Ref object for the node }).exec()}})
The method is used as follows:SelectorQuery NodesRef.boundingClientRect(function callback)
getBoundingClientRect
of the DOM.Attributes | Types | Note |
id | string | Node's ID |
dataset | Object | Node's dataset |
left | number | Node's left boundary coordinate |
right | number | Node's right boundary coordinate |
top | number | Node's upper boundary coordinate |
bottom | number | Node's lower boundary coordinate |
width | number | Node's width |
height | number | Node's height |
Page({getRect() {wx.createSelectorQuery().select('#the-id').boundingClientRect(function (rect) {rect.id // Node's IDrect.dataset // Node's datasetrect.left // Node's left boundary coordinaterect.right // Node's right boundary coordinaterect.top // Node's upper boundary coordinaterect.bottom // Node's lower boundary coordinaterect.width // Node's widthrect.height // Node's height}).exec()},getAllRects() {wx.createSelectorQuery().selectAll('.a-class').boundingClientRect(function (rects) {rects.forEach(function (rect) {rect.id // Node's IDrect.dataset // Node's datasetrect.left // Node's left boundary coordinaterect.right // Node's right boundary coordinaterect.top // Node's upper boundary coordinaterect.bottom // Node's lower boundary coordinaterect.width // Node's widthrect.height // Node's height})}).exec()}})
The method is utilized as SelectorQuery NodesRef.scrollOffset(function callback)
scroll-view
or a viewport
.Attributes | Types | Note |
id | string | Node's ID |
dataset | Object | Node's dataset |
scrollLeft | number | Node's horizontal scroll position |
scrollTop | number | Node's vertical scroll position |
Page({getScrollOffset() {wx.createSelectorQuery().selectViewport().scrollOffset(function (res) {res.id // Node's IDres.dataset // Node's datasetres.scrollLeft // Node's horizontal scroll positionres.scrollTop // Node's vertical scroll position}).exec()}})
The method is utilized as follows: SelectorQuery NodesRef.context(function callback)
Attributes | Types | Note |
context | Object | Context object corresponding to the node |
Page({getContext() {wx.createSelectorQuery().select('.the-video-class').context(function (res) {console.log(res.context) // Context object corresponding to the node. For instance, if the selected node is a <map> component, then a MapContext object is returned here.}).exec()}})
The method is employed as follows: SelectorQuery NodesRef.node(NodesRef.nodeCallback callback)
Attributes | Types | Note |
node | Object | Node instance corresponding to the node |
Page({getNode() {wx.createSelectorQuery().select('.canvas').node(function(res){console.log(res.node) // Canvas instance corresponding to the node.}).exec()}})
The method is utilized as follows: NodesRef SelectorQuery.exec(function callback)
The method is utilized as follows: SelectorQuery SelectorQuery.in(Component component)
Component({queryMultipleNodes() {const query = wx.createSelectorQuery().in(this)query.select('#the-id').boundingClientRect(function (res) {res.top // The upper boundary coordinate of the #the-id node within this component.}).exec()}})
The method is utilized as follows: NodesRef SelectorQuery.select(string selector)
The method is utilized as follows: NodesRef SelectorQuery.selectAll(string selector)
The method is utilized as follows: NodesRef SelectorQuery.selectViewport()
The method is utilized as follows: MediaQueryObserver.disconnect()
The method is utilized as follows: MediaQueryObserver.observe(Object descriptor, function callback)
Attributes | Types | Default value | Required | Note |
minWidth | number | - | Supported | Minimum page width (in pixels) |
maxWidth | number | - | Supported | Maximum page width (in pixels) |
width | number | - | Supported | Page width (in pixels) |
minHeight | number | - | Supported | Minimum page height (in pixels) |
maxHeight | number | - | Supported | Maximum page height (in pixels) |
height | number | - | Supported | Page height (in pixels) |
orientation | string | - | Supported | Screen orientation (landscape or portrait) |
Attributes | Types | Note |
matches | boolean | Whether the current status of the page satisfies the specified media query. |
Was this page helpful?