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

in-sight-detector

v1.0.5

Published

DOM in sight detector

Downloads

10

Readme

试试 in-sight-detector.js 呗,用来动态检测某些 DOM 元素是否处于浏览器窗口内。

可以用来:

  • 根据屏幕滚动动态加载元素
  • 统计某些动态变化的元素的展现量

github 只是用来存放源码,如果想要获取生产环境所需的代码,可以通过以下两种形式:

  • npm
  npm install in-sight-detector
  • 本地编译
git clone https://github.com/dblate/in-sight-detector.git

cd in-sight-detector

npm install

grunt

grunt 执行完后在 dist 目录下有编译完成的代码

    import InSightDetector from 'in-sight-detector'
    
    const detector = new InSightDetector();
    const testEl = document.getElementById('test-el');
    
    detector.addListener(testEl, () => {
        // testEl 出现在视图中时触发
        console.log('TestEl has shown');
        
        // 有时你需要装完哔就跑
        detector.removeListener(testEl);
    });


    // 同时为多个元素绑定事件
    detector.addListener([el0, el1, el2], () => {
        console.log('Bind multiple elements at the same time');
    });

    // 同时为多个元素取消事件
    detector.removeListener([el0, el1, el2]);

    // 移除所有元素及其事件
    detector.removeAllListener();

in-sight-detector.md

使用了 addEventListener 和 requestAnimationFrame,pc 端不兼容 IE,移动端没问题

一个页面可能会被分为多个模块,如果用 Class 创建 detector 实例,则每个 detector 的数据是独立的。反之,数据是公共的,容易误操作别人的数据

整个事件是在浏览器滚动时执行的,浏览器花了很多精力去解决滚动时的性能问题,若是因为你的一两行代码搞得页面滑动时卡顿,会有点不划算(当然一般不会这样,除非你有很多回调,而且做很多丧心病狂的操作)。

in-sight-detector.js 中通过一些手段尽量保证性能:

  • 使用 requestAnimationFrame,让浏览器渲染优先
  • 提供 interval 参数为函数提供最小执行间隔

但决定因素还是在使用者身上,建议:

  • 回调函数中不要做太耗性能的操作,比如频繁的修改 DOM
  • 如果有时确实需要做一些有损性能的操作,可以把 interval 值稍微调大一点
  • 某些元素不再需要检测时,移除对应的事件