Code Monkey home page Code Monkey logo

intersection-observer-admin's Introduction

intersection-observer-admin

Download count all time npm version

Why use an administrator to manage all the elements on my page?

This library is used in ember-in-viewport and 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 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

API

  1. 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
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();

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, 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.

intersection-observer-admin's People

Contributors

chriskrycho avatar dependabot[bot] avatar maeertin avatar snewcomer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

intersection-observer-admin's Issues

[Typescript] threshold wrongly typed?

Hi there,

Could it be that threshold in the IOptions interface is wrongly typed? Not fluent in Typescript :)

export interface IOptions {
root?: HTMLElement;
rootMargin?: string;
threshold?: number;
[key: string]: any;
}

The typing above says it should be of type number but the IntersectionObserver API allows threshold to be an array of numbers. Should it not be threshold?: number | number[]?

if (Array.isArray(threshold)) {
threshold = threshold[threshold.length - 1];
}

You seem to already handle somehow threshold as an array.

Thanks!

removeCallback API

Is it possible to add the remove equivalent to the enter and exit callbacks?

It is possible for us to want to remove the listener when limiting the number of times a callback can fire without unobserving the element.

Doesn't do anything if already have match for root

@snewcomer looks like changes in #28 cause an issue:
when we observe two elements with different options but the same root, only first element is "watched".

I've setup simple reproduction repo where you can see the issue.

Here is the commit which adds the problematic code:

  • we have two elements
  • both elements share same root (root is implicitly window)
  • two elements have different rootMargin via viewportTolerance
  • only first element is watched, the second one is ignored.

Couldn't grasp what bug #28 fixes to create a PR hence would love to hear your @snewcomer thoughts on this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.