WXML Node Information
The Node Information Query API (createSelectorQuery) allows you to retrieve node properties, styles, and position on the screen, among other details.
The most common usage is to query the current position of a specific node and the scroll position of the viewport.
Page({
onLoad: function(){
const query = this.createSelectorQuery()
query.select('#the-id').boundingClientRect(function(res){
res.top // Top boundary coordinate of the #the-id node (relative to the viewport)
})
query.selectViewport().scrollOffset(function(res){
res.scrollTop // Vertical scroll position of the viewport
})
query.exec()
}
})
In the example above, #the-id is a node selector. It is similar to CSS selectors but has slight differences. Please refer to the relevant documentation for SelectorQuery.select.
wx.createSelectorQuery is currently deprecated. You should use this.createSelectorQuery instead.
Intersection State of WXML Nodes
The intersection state API createIntersectionObserver can be used to monitor the intersection state of two or more component nodes in terms of their layout positions. These APIs are commonly used to infer whether specific nodes are visible to the user and what proportion of them is visible.
The main concepts involved in these APIs are as follows.
- Reference Node: The node used as the reference for monitoring. Its layout area is used as the reference area. If there are multiple reference nodes, the intersection of their layout areas is used as the reference area. The page viewport can also be used as one of the reference areas.
- Target Node: The object of monitoring. By default, only one node can be monitored (when using the
selectAlloption, multiple nodes can be monitored simultaneously). - Intersection Area: The intersection area between the layout area of the target node and the reference area.
- Intersection Ratio: The proportion of the intersection area relative to the layout area of the target node.
- Thresholds: If the intersection ratio reaches a threshold, the callback function of the observer is triggered. Multiple thresholds can be set.
The following example code triggers the callback function every time the target node (specified by the selector .target-class) enters or leaves the page viewport. For example:
Page({
onLoad: function(){
this.createIntersectionObserver().relativeToViewport().observe('.target-class', (res) => {
res.id // Target node id
res.dataset // Target node dataset
res.intersectionRatio // Ratio of the intersection area to the target node's layout area
res.intersectionRect // Intersection area
res.intersectionRect.left // Left boundary coordinate of the intersection area
res.intersectionRect.top // Top boundary coordinate of the intersection area
res.intersectionRect.width // Width of the intersection area
res.intersectionRect.height // Height of the intersection area
})
}
})
The following example code triggers the callback function when the target node (specified by the selector .target-class) intersects or separates from the reference node (specified by the selector .relative-class) within the page viewport, and the degree of intersection or separation reaches 20% and 50% of the target node's layout area.
Page({
onLoad: function(){
this.createIntersectionObserver({
thresholds: [0.2, 0.5]
}).relativeTo('.relative-class').relativeToViewport().observe('.target-class', (res) => {
res.intersectionRatio // Ratio of the intersection area to the target node's layout area
res.intersectionRect // Intersection area
res.intersectionRect.left // Left boundary coordinate of the intersection area
res.intersectionRect.top // Top boundary coordinate of the intersection area
res.intersectionRect.width // Width of the intersection area
res.intersectionRect.height // Height of the intersection area
})
}
})
Note: The intersection area with the page viewport does not accurately represent the area visible to the user, because the area involved in the calculation is the "layout area." This layout area may be clipped or hidden during rendering by other nodes (e.g., ancestor nodes with overflow style set to hidden) or obscured (e.g., nodes with fixed positioning).
wx.createIntersectionObserver is currently deprecated. You should use this.createIntersectionObserver instead.