has.createIntersectionObserver(Object component, Object options): IntersectionObserver
Creates and returns an IntersectionObserver object instance.
The ASCF observer uses the web native mode, which is different from the third-party observer. The differences are as follows:
The reference area set by the web native observer by calling relativeTo cannot exceed the range of the node area. For details about the web native observer, see IntersectionObserver Document.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| component | Object | Yes | Custom component instance. |
| options | Object | No | Options. |
options
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| thresholds | Array<number> | [0] | No | A numeric array that contains all thresholds. The value range is [0, 1.0]. |
| initialRatio | number | 0 | No | Initial intersection ratio. The value range is [0, 1.0]. If the detected intersection ratio is different from the specified value and reaches the threshold, the callback of the observer is triggered. |
| observeAll | boolean | false | No | Whether to observe multiple target nodes at the same time. If the value is true, the targetSelector of the observer selects multiple nodes. Note that when too many nodes are selected at the same time, the rendering performance is affected. |
Returns:
IntersectionObserver object.
Example:
const observer = has.createIntersectionObserver(this, {
thresholds: [0],
initialRatio: 0,
observeAll: true
}); Represents an IntersectionObserver object, which is used to determine whether certain nodes are displayed and what percentage of the nodes are displayed.
Since: 1.0.4
IntersectionObserver.disconnect()
Stops listening. The callback will not be triggered.
Since: 1.0.4
Example:
const observer = has.createIntersectionObserver(this, {
thresholds: [0],
initialRatio: 0,
observeAll: true
});
observer.disconnect(); IntersectionObserver.observe(string targetSelector, function callback)
Specifies the target node and starts to observe the intersection status change.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| targetSelector | string | Yes | Selector. |
| callback | function | No | Callback used for listening for the intersection status change. |
Return value of callback
An object containing the following fields.
| Parameter | Type | Description |
|---|---|---|
| id | string | Node ID. |
| dataset | Record<string,any> | Custom data attributes of a node. |
| intersectionRatio | number | Intersection ratio. |
| intersectionRect | Object | Margin of the intersection area. |
| boundingClientRect | Object | Target margin. |
| relativeRect | Object | Margin of the reference area. |
| time | number | Timestamp of intersection detection. |
intersectionRect
| Parameter | Type | Description |
|---|---|---|
| left | number | Left margin. |
| right | number | Right margin. |
| top | number | Top margin. |
| bottom | number | Bottom margin. |
| width | number | Width. |
| height | number | Height. |
boundingClientRect
| Parameter | Type | Description |
|---|---|---|
| left | number | Left margin. |
| right | number | Right margin. |
| top | number | Top margin. |
| bottom | number | Bottom margin. |
| width | number | Width. |
| height | number | Height. |
relativeRect
| Parameter | Type | Description |
|---|---|---|
| left | number | Left margin. |
| right | number | Right margin. |
| top | number | Top margin. |
| bottom | number | Bottom margin. |
| width | number | Width. |
| height | number | Height. |
Example:
const observer = has.createIntersectionObserver(this, {
thresholds: [0],
initialRatio: 0,
observeAll: true
});
observer
.relativeTo('.scroll-view_H', {
left: -50,
right: -50
})
.observe('.demo-text-2', (res) => {
console.info('observe:', res);
}); IntersectionObserver.relativeTo(string selector, Object margins): IntersectionObserver
Specifies a node using a selector as one of the reference areas.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| selector | string | Yes | Selector. |
| margins | object | No | Margins used to extend (or shrink) the layout area of the reference node. |
margins
| Parameter | Type | Description |
|---|---|---|
| left | number | Left margin of the node layout area. |
| right | number | Right margin of the node layout area. |
| top | number | Top margin of the node layout area. |
| bottom | number | Bottom margin of the node layout area. |
Returns:
IntersectionObserver object.
Example:
const observer = has.createIntersectionObserver(this, {
thresholds: [0],
initialRatio: 0,
observeAll: true
});
observer
.relativeTo('.scroll-view_H', {
left: -50,
right: -50
})
.observe('.demo-text-2', (res) => {
console.info('observe:', res);
}); IntersectionObserver.relativeToViewport(Object margins): IntersectionObserver
Specifies the page display area as one of the reference areas.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| margins | object | No | Margins used to extend (or shrink) the layout area of the reference node. |
margins
| Parameter | Type | Description |
|---|---|---|
| left | number | Left margin of the node layout area. |
| right | number | Right margin of the node layout area. |
| top | number | Top margin of the node layout area. |
| bottom | number | Bottom margin of the node layout area. |
Returns:
IntersectionObserver object.
Example:
const observer = has.createIntersectionObserver(this, {
thresholds: [0],
initialRatio: 0,
observeAll: true
});
observer
.relativeToViewport({
left: -50,
right: -50
})
.observe('.demo-text-2', (res) => {
console.info('observe:', res);
});