Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/snewcomer/intersection-observer-admin
Intersection Observer Admin for better performance
https://github.com/snewcomer/intersection-observer-admin
intersection-observer intersectionobserver-api lazy-loading lazyload-images performance
Last synced: 7 days ago
JSON representation
Intersection Observer Admin for better performance
- Host: GitHub
- URL: https://github.com/snewcomer/intersection-observer-admin
- Owner: snewcomer
- Created: 2018-11-18T16:03:56.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-16T02:02:28.000Z (4 months ago)
- Last Synced: 2024-10-30T08:18:26.563Z (21 days ago)
- Topics: intersection-observer, intersectionobserver-api, lazy-loading, lazyload-images, performance
- Language: TypeScript
- Size: 1.61 MB
- Stars: 38
- Watchers: 2
- Forks: 7
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
intersection-observer-admin
==============================================================================
![Download count all time](https://img.shields.io/npm/dt/intersection-observer-admin.svg)
[![npm version](https://badge.fury.io/js/intersection-observer-admin.svg)](http://badge.fury.io/js/intersection-observer-admin)Why use an administrator to manage all the elements on my page?
------------------------------------------------------------------------------
This library is used in [ember-in-viewport](https://github.com/DockYard/ember-in-viewport) and [ember-infinity](https://github.com/ember-infinity/ember-infinity). This library is particularly important for re-using the IntersectionObserver API.Most implementations have one Intersection Observer for each target element or so called `sentinel`. However, [IntersectionObserver.observe](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/observe) can observer multiple `sentinels`. So this library will resuse the IntersectionObserver instance for another element on the page with the same set of observer options and root element. This can dramatically improve performance for pages with lots of elements and observers.
_Note: A companion library is also available for requestAnimationFrame: https://github.com/snewcomer/raf-pool_
Installation
------------------------------------------------------------------------------```
npm install intersection-observer-admin --save
```Usage
------------------------------------------------------------------------------
## API1. element: DOM Node to observe
2. enterCallback: Function
- callback function to perform logic in your own application
3. exitCallback: Function
- callback function to perform when element is leaving the viewport
4. observerOptions: Object
- list of options to pass to Intersection Observer constructor (https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver)```js
import IntersectionObserverAdmin from 'intersection-observer-admin';const intersectionObserverAdmin = new IntersectionObserverAdmin();
// Add callbacks that will be called when observer detects entering and leaving viewport
intersectionObserverAdmin.addEnterCallback(element, enterCallback);
intersectionObserverAdmin.addExitCallback(element, exitCallback);// add an element to static administrator with window as scrollable area
intersectionObserverAdmin.observe(element, { root, rootMargin: '0px 0px 100px 0px', threshold: 0 });// add an element in a scrolling container
intersectionObserverAdmin.observe(element, { root, rootMargin: '0px 0px 100px 0px', threshold: 0 });// Use in cleanup lifecycle hooks (if applicable) from the element being observed
intersectionObserverAdmin.unobserve(element, observerOptions);// Use in cleanup lifecycle hooks of your application as a whole
// This will remove the in memory data store holding onto all of the observers
intersectionObserverAdmin.destroy();
```[IntersectionObserver's Browser Support](https://platform-status.mozilla.org/)
------------------------------------------------------------------------------
### Out of the box
Chrome
51 [1]
Firefox (Gecko)
55 [2]
MS Edge
15
Internet Explorer
Not supported
Opera [1]
38
Safari
12.1
Chrome for Android
59
Android Browser
56
Opera Mobile
37
* [1] [Reportedly available](https://www.chromestatus.com/features/5695342691483648), it didn't trigger the events on initial load and lacks `isIntersecting` until later versions.
* [2] This feature was implemented in Gecko 53.0 (Firefox 53.0 / Thunderbird 53.0 / SeaMonkey 2.50) behind the preference `dom.IntersectionObserver.enabled`.