@uni/intersection-observer
v1.0.8
Published
Downloads
14
Readme
intersectionObserver
Return An IntersectionObserver object that infers whether and how likely certain nodes are visible to users.
Supported
Install
$ npm install @uni/intersectionObserver --save
or
$ npm install @uni/apis --save
Usage
import createIntersectionObserver from '@uni/intersection-observer';
const intersectionObserver = createIntersectionObserver({
thresholds: [0]
});
intersectionObserver.relativeTo('.box').observe('.circle', res => {
console.log(res);
});
You can also import from the big package:
import { intersectionObserver } from '@uni/apis';
const observer = intersectionObserver({
thresholds: [0]
});
observer.relativeTo('.box').observe('.circle', res => {
console.log(res);
});
Methods
createIntersectionObserver(options, context?)
Arguments
| Property | Type | Description | required | Default |
| --- | --- | --- | --- | --- |
| options | object
| | ✘ | - |
| options.thresholds | Array<number>
| An array of values, whichcontains all thresholds | ✘ | [0] |
| options.initialRatio | number
| The initial intersection ratio. If the intersection ratio detected at the time of the call is not equal to this value and reaches the threshold, the callback function of the listener is triggered. | ✘ | 0 |
| options.selectAll | boolean
| Indicates whether to observe more than one target node simultaneously. If the value is set to "true", the targetSelector of observe will select multiple nodes. (Note: selecting too many nodes at the same time will affect rendering performance.) | ✘ | false |
| component | object
| Custom component instance | ✘ | - |
Return
| Property | Type | Description |
| --- | --- | --- |
| IntersectionObserver | Object
| IntersectionObserver |
IntersectionObserver.relativeTo(string selector, Object margins)
Uses a selector to specify a node as one of the reference areas.
Arguments
| Property | Type | Description | required | Default |
| --- | --- | --- | --- | --- |
| selector | string
| Selector | ✔️ | - |
| margins | object
| Expands/Contracts the border of the layout area of the reference node. | ✘ | - |
| margins.left | number
| Left border of the node layout area | ✘ | 0 |
| margins.top | number
| Upper border of the node layout area | ✘ | 0 |
| margins.right | number
| Right border of the node layout area | ✘ | 0 |
| margins.bottom | number
| Lower border of the node layout area | ✘ | 0 |
Return
| Property | Type | Description |
| --- | --- | --- |
| IntersectionObserver | Object
| IntersectionObserver |
IntersectionObserver.relativeToViewport(Object margins)
Specifies the page display area as one of the reference areas.
Arguments
| Property | Type | Description | required | Default |
| --- | --- | --- | --- | --- |
| margins | object
| Expands/Contracts the border of the layout area of the reference node. | ✘ | - |
| margins.left | number
| Left border of the node layout area | ✘ | 0 |
| margins.top | number
| Upper border of the node layout area | ✘ | 0 |
| margins.right | number
| Right border of the node layout area | ✘ | 0 |
| margins.bottom | number
| Lower border of the node layout area | ✘ | 0 |
Return
| Property | Type | Description |
| --- | --- | --- |
| IntersectionObserver | Object
| IntersectionObserver |
IntersectionObserver.observe(string targetSelector, function callback)
Specifies the target node and starts listening on changes in the intersection status.
Arguments
| Property | Type | Description | required | Default |
| --- | --- | --- | --- | --- |
| targetSelector | string
| Selector | ✔️ | - |
| callback | Function
| The callback function for listening on intersection status changes. | ✘ | - |
callback res
| Property | Type | Description |
| --- | --- | --- |
| intersectionRatio | number
| Intersection ratio |
| intersectionRect | Object
| The border of the intersection area |
| boundingClientRect | Object
| The target border |
| relativeRect | Object
| The border of the reference area |
| time | number
| The timestamp for intersection detection |
intersectionRect、boundingClientRect
| Property | Type | Description |
| --- | --- | --- |
| left | number
| Left border |
| top | number
| Upper border |
| right | number
| Right border |
| bottom | number
| Lower border |
| width | number
| width |
| height | number
| height |
relativeRect
| Property | Type | Description |
| --- | --- | --- |
| left | number
| Left border |
| top | number
| Upper border |
| right | number
| Right border |
| bottom | number
| Lower border |
Return
| Property | Type | Description |
| --- | --- | --- |
| IntersectionObserver | Object
| IntersectionObserver |
IntersectionObserver.disconnect()
Stops listening, and the callback function will no longer be triggered.
注意
所有方法在微信小程序、百度小程序的自定义组件中使用的时候,都需要添加第二个参数来指定自定义组件实例:
// 在 Rax 小程序编译时链路参数为 this._internal
createIntersectionObserver({thresholds: [0]}, this);
// rax小程序基于kbone的运行时方案,不支持relativeTo,微信端使用时需要按下方示例方式传入实例
useEffect(() => {
const node = document.querySelector('#circle');
const intersectionObserver = createIntersectionObserver({}, node._internal);
intersectionObserver.relativeToViewport().observe('#circle', res => {
console.log(res);
setAppear(res.intersectionRatio > 0);
});
}, []);
为保证多端可用,可参考demo实现
useEffect(() => {
// 阿里小程序需在 page.onReady 之后执行 createIntersectionObserver(),setTimeout 可延迟执行时机
setTimeout(() => {
// node必须为block元素和circle元素的共同父元素
const node = document.querySelector('.parent');
const intersectionObserver = createIntersectionObserver(null, node._internal);
// 由于rax运行时在微信存在shadow dom问题,所以采用深度选择器
const clsPre = isWeChatMiniProgram ? '.parent >>> ' : '';
intersectionObserver.relativeTo(clsPre + '.block').observe(clsPre + '.circle', res => {
console.log(res);
setAppear(res.intersectionRatio > 0);
});
}, 0);
}, []);