2.0.0-beta • Published 3 years ago

universal-intersection-observer v2.0.0-beta

Weekly downloads
2
License
BSD-3-Clause
Repository
-
Last release
3 years ago

intersectionObserver

npm npm

Return An IntersectionObserver object that infers whether and how likely certain nodes are visible to users.

Supported

Install

$ npm install universal-intersectionObserver --save

or

$ npm install universal-api --save

Usage

import createIntersectionObserver from 'universal-intersectionObserver';

const intersectionObserver = createIntersectionObserver({
  component: null,
  options: {
    thresholds: [0]
  }
});

intersectionObserver.relativeTo('.box').observe('.circle', res => {
  console.log(res);
});

You can also import from the big package:

import { intersectionObserver } from 'universal-api';

const observer = intersectionObserver({
  options: {
    thresholds: [0]
  }
});

observer.relativeTo('.box').observe('.circle', res => {
  console.log(res);
});

Methods

createIntersectionObserver(component, options)

Arguments

PropertyTypeDescriptionrequiredDefault
optionsobject -
options.thresholdsArray<number>An array of values, which​contains all thresholds0
options.initialRationumberThe 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.observeAllboolean 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
componentobject Custom component instance-

Return

PropertyTypeDescription
IntersectionObserverObjectIntersectionObserver

IntersectionObserver.relativeTo(string selector, Object margins)

Uses a selector to specify a node as one of the reference areas.

Arguments

PropertyTypeDescriptionrequiredDefault
selectorstringSelector✔️-
marginsobject Expands/Contracts the border of the layout area of the reference node.-
margins.leftnumberLeft border of the node layout area-
margins.topnumberUpper border of the node layout area-
margins.rightnumber Right border of the node layout area-
margins.bottomnumber Lower border of the node layout area-

Return

PropertyTypeDescription
IntersectionObserverObjectIntersectionObserver

IntersectionObserver.relativeToViewport(Object margins)

Specifies the page display area as one of the reference areas.

Arguments

PropertyTypeDescriptionrequiredDefault
marginsobject Expands/Contracts the border of the layout area of the reference node.-
margins.leftnumberLeft border of the node layout area-
margins.topnumberUpper border of the node layout area-
margins.rightnumber Right border of the node layout area-
margins.bottomnumber Lower border of the node layout area-

Return

PropertyTypeDescription
IntersectionObserverObjectIntersectionObserver

IntersectionObserver.observe(string targetSelector, function callback)

Specifies the target node and starts listening on changes in the intersection status.

Arguments

PropertyTypeDescriptionrequiredDefault
targetSelectorstringSelector✔️-
callbackFunctionThe callback function for listening on intersection status changes.-

callback res

PropertyTypeDescription
intersectionRationumberIntersection ratio
intersectionRectObjectThe border of the intersection area
boundingClientRectObject The target border
relativeRectObject The border of the reference area
timenumber The timestamp for intersection detection

intersectionRect、boundingClientRect

PropertyTypeDescription
leftnumberLeft border
topnumberUpper border
rightnumber Right border
bottomnumber Lower border
widthnumber width
heightnumber height

relativeRect

PropertyTypeDescription
leftnumberLeft border
topnumberUpper border
rightnumber Right border
bottomnumber Lower border

Return

PropertyTypeDescription
IntersectionObserverObjectIntersectionObserver

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);
  });
}, []);
/**
 * iframe: true
 */
import React from 'react';
export default () => (
  <iframe style={{
      boxShadow: '0 2px 15px rgba(0,0,0,0.1)',
      width: '375px',
      height: '700px'
    }} src='https://herbox.online/p/109000004/app_1aKtEd7SK?previewZoom=100&view=preview&defaultPage=pages/universal-intersection-observer/index&topSlider=false'></iframe>
);