{"id":15578641,"url":"https://github.com/dblate/in-sight-detector","last_synced_at":"2025-10-29T05:37:38.276Z","repository":{"id":57273186,"uuid":"123575978","full_name":"dblate/in-sight-detector","owner":"dblate","description":"动态检测某些 DOM 元素是否处于浏览器窗口之内","archived":false,"fork":false,"pushed_at":"2018-03-12T05:58:10.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T06:04:22.911Z","etag":null,"topics":["dom","javascript"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dblate.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-02T12:22:16.000Z","updated_at":"2023-10-19T09:41:40.000Z","dependencies_parsed_at":"2022-08-25T07:14:10.665Z","dependency_job_id":null,"html_url":"https://github.com/dblate/in-sight-detector","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dblate%2Fin-sight-detector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dblate%2Fin-sight-detector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dblate%2Fin-sight-detector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dblate%2Fin-sight-detector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dblate","download_url":"https://codeload.github.com/dblate/in-sight-detector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246156029,"owners_count":20732359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dom","javascript"],"created_at":"2024-10-02T19:11:58.414Z","updated_at":"2025-10-29T05:37:33.226Z","avatar_url":"https://github.com/dblate.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1\u003eInSightDetector\u003c/h1\u003e\n\n\n\u003ch3\u003e使用情景\u003c/h3\u003e\n是否遇到过这种情况：需要检测一个元素是否展示 =\u003e 该元素不在首屏 =\u003e 页面滑动过程中 js 动态改变了其它元素，导致该元素的位置又变了？\n\n试试 in-sight-detector.js 呗，用来动态检测某些 DOM 元素是否处于浏览器窗口内。\n\n可以用来：\n\n* 根据屏幕滚动动态加载元素\n* 统计某些动态变化的元素的展现量\n\n\n\u003ch3\u003e快速使用\u003c/h3\u003e\n\n\u003ch4\u003e获取代码\u003c/h4\u003e\n\ngithub 只是用来存放源码，如果想要获取生产环境所需的代码，可以通过以下两种形式：\n\n* npm\n\n```javascript\n  npm install in-sight-detector\n```\n\n* 本地编译\n\n```shell\ngit clone https://github.com/dblate/in-sight-detector.git\n\ncd in-sight-detector\n\nnpm install\n\ngrunt\n```\n\ngrunt 执行完后在 dist 目录下有编译完成的代码\n\n\u003ch4\u003e使用示例\u003c/h4\u003e\n\n```javascript\n    import InSightDetector from 'in-sight-detector'\n    \n    const detector = new InSightDetector();\n    const testEl = document.getElementById('test-el');\n    \n    detector.addListener(testEl, () =\u003e {\n        // testEl 出现在视图中时触发\n        console.log('TestEl has shown');\n        \n        // 有时你需要装完哔就跑\n        detector.removeListener(testEl);\n    });\n\n\n    // 同时为多个元素绑定事件\n    detector.addListener([el0, el1, el2], () =\u003e {\n        console.log('Bind multiple elements at the same time');\n    });\n\n    // 同时为多个元素取消事件\n    detector.removeListener([el0, el1, el2]);\n\n    // 移除所有元素及其事件\n    detector.removeAllListener();\n```\n\n\u003ch3\u003e文档\u003c/h3\u003e\n\n[in-sight-detector.md](https://github.com/dblate/in-sight-detector/blob/master/docs/in-sight-detector.md)\n\n\u003ch3\u003e兼容性\u003c/h3\u003e\n\n使用了 addEventListener 和 requestAnimationFrame，pc 端不兼容 IE，移动端没问题\n\n\u003ch3\u003e一些想法\u003c/h3\u003e\n\n\u003ch4\u003e为什么要用 Class 而不用普通的函数或者对象？\u003c/h4\u003e\n\n一个页面可能会被分为多个模块，如果用 Class 创建 detector 实例，则每个 detector 的数据是独立的。反之，数据是公共的，容易误操作别人的数据\n  \n\u003ch4\u003e别影响浏览器性能\u003c/h4\u003e\n\n整个事件是在浏览器滚动时执行的，浏览器花了很多精力去解决滚动时的性能问题，若是因为你的一两行代码搞得页面滑动时卡顿，会有点不划算（当然一般不会这样，除非你有很多回调，而且做很多丧心病狂的操作）。\n\nin-sight-detector.js 中通过一些手段尽量保证性能：\n\n* 使用 requestAnimationFrame，让浏览器渲染优先\n* 提供 interval 参数为函数提供最小执行间隔\n\n但决定因素还是在使用者身上，建议：\n\n* 回调函数中不要做太耗性能的操作，比如频繁的修改 DOM\n* 如果有时确实需要做一些有损性能的操作，可以把 interval 值稍微调大一点\n* 某些元素不再需要检测时，移除对应的事件\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdblate%2Fin-sight-detector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdblate%2Fin-sight-detector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdblate%2Fin-sight-detector/lists"}