npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@uni/intersection-observer

v1.0.8

Published

Downloads

16

Readme

intersectionObserver

npm

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, which​contains 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);
  }, []);