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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-lazyload-typescript

v0.0.4

Published

Inspired by react-lazyload, write in typescript

Downloads

1

Readme

react-lazyload 启发, 使用 typescript 开发

原项目地址

https://github.com/twobin/react-lazyload

使用方式也可参考

注意事项

  • perf: 优化合并了 checkNormalVisiblecheckOverflowVisible 方法,因为实现原理是一致的。checkOverflowVisible 是另一个方法的超集。另外原来 checkNormalVisible 中高度都取自 window 并没有来自 scrollContainer 的对应 dom,其实是有 bug 的。

  • perf: 优化了 checkVisible 的实现,老代码中,无论是否启用 overflow 属性,都会去遍历寻找滚动的父元素的,这是消耗性能的。

      // 老代码
      const parent = scrollParent(node);
      const isOverflow =
          component.props.overflow &&
          parent !== node.ownerDocument &&
          parent !== document &&
          parent !== document.documentElement;
          const visible = isOverflow
      ? checkOverflowVisible(component, parent)
      : checkNormalVisible(component);
    
      // 新代码
      const visible = checkElementVisible(component, component.parent);
  • perf: 优化了 once 的实现。

    • 调整了 purgePending() 的执行顺序,因为 pending 中的元素是已经展示了,才被添加到 pending 中的,所以可以在 checkVisible 前执行。

    • 另外修改了 once 默认为 true, 因为我的项目只是单纯的想做懒加载,没有防爬虫和复杂列表性能问题,所以修改为 true,而且我认为更高性能的默认值可以降低使用者的使用成本。

      // 老代码
      const lazyLoadHandler = () => {
      for (let i = 0; i < listeners.length; ++i) {
      const listener = listeners[i];
      checkVisible(listener);
      }
      purgePending();
      }
      
      // 新代码
      const lazyLoadHandler = () => {
          purgePending();
          for (let i = 0; i < listeners.length; ++i) {
              const listener = listeners[i];
              checkVisible(listener);
          }
      }
      
      // 老代码 props.once 默认值 false
      // 新代码 props.once 默认值 true
  • perf: 优化了 overflow 的实现,对 parent 的赋值放到了 componentDidMount 中,而不用在 checkVisible 时,每次都去找父滚动容器。

  • perf&break-change: 简化了 scrollParent 的实现

    • 如果想用 overflow 属性来自动寻找父滚动容器的方法,需要保证子元素的第一个能找到的 overflow-x|-y autoscroll 的就是滚动容器,原代码中对 overflow 的校验是 x, y 都要是自动或滚动,不太符合我的场景。

    • 目前项目中想自动寻找父容器的原因是,公共组件在不同路由使用,可能多个路由的滚动容器类名不同,如果每个路由都要单独传 scrollContainer 改造成本太大,所以采用自动寻找模式

      // 新代码
      const findScrollParent = (node: HTMLElement | null): HTMLElement => {
          if (!node || !(node instanceof HTMLElement)) {
              return document.documentElement;
          }
      
          const overflowRegex = /(auto|scroll)/;
      
          let parent = node.parentElement;
          while (parent) {
              const { overflow, overflowX, overflowY } = window.getComputedStyle(parent);
      
              if (overflowRegex.test(overflow) || overflowRegex.test(overflowX) || overflowRegex.test(overflowY)) {
                  return parent;
              }
      
              parent = parent.parentElement;
          }
      
          return node.ownerDocument?.documentElement || document.documentElement;
      };
  • break-change:移除了 throttledebounce 实现,因为很多项目都有自己的防抖和截流,而且每个子元素都可以修改父容器公共的 scroll 事件,其实不太科学,增加了绑定,解绑 scroll 事件的复杂度,如果想实现,可能会再增加全局方法,可以对 scroll 事件做统一的防抖,截流设置。

  • fix:修复了占位消失时,图片未出现,导致 getBoundingClientRect 取值错误的问题

  • break-change: 移除了 forceCheck , forceVisible, 装饰器等功能