Coder Social home page Coder Social logo

zeusdeux / observe-element-in-viewport Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 1.0 93 KB

Wrapper around IntersectionObserver to hide away its weirdness and increase ease of usability

TypeScript 100.00%
viewport intersection-observer intersectionobserver

observe-element-in-viewport's Introduction

observe-element-in-viewport

In browsers that support the Intersection Observer, this library lets you find out when an element of yours is in viewport. The viewport itself is configurable. It can be the window or any other element in the DOM. Only requirement is that the element who visibility you want to check must be a child of the element you choose as your viewport.

Installation

npm install observe-element-in-viewport

or grab it directly from unpkg and include it in your html as

<!-- will fetch UMD build -->
<script src="https://unpkg.com/observe-element-in-viewport"></script>
<!-- or below to fetch the es module build -->
<script type="module" src="https://unpkg.com/observe-element-in-viewport?module"></script>

or directly in your javascript as below which will import the latest version of observe-element-in-viewport.

import {
  observeElementInViewport,
  isInViewport
} from 'https://unpkg.com/observe-element-in-viewport?module'

Note

For more information on how to get specific versions from unpkg, have a look at their webpage.

Usage

Observing a target element

import { observeElementInViewport } from 'observe-element-in-viewport'

// to use window as viewport, pass this option as null
const viewport = document.querySelector('.viewport')

// element whose visibility we want to track
const target = document.querySelector('.target')

// handler for when target is in viewport
const inHandler = (entry, unobserve, targetEl) => console.log('In viewport')

// handler for when target is NOT in viewport
const outHandler = (entry, unobserve, targetEl) => console.log('Not in viewport')

// the returned function, when called, stops tracking the target element in the
// given viewport
const unobserve = observeElementInViewport(target, inHandler, outHandler, {
  // set viewport
  viewport,

  // decrease viewport top by 100px
  // similar to this, modRight, modBottom and modLeft exist
  modTop: '-100px'

  // threshold tells us when to trigger the handlers.
  // a threshold of 90 means, trigger the inHandler when atleast 90%
  // of target is visible. It triggers the outHandler when the amount of
  // visible portion of the target falls below 90%.
  // If this array has more than one value, the lowest threshold is what
  // marks the target as having left the viewport
  threshold: [90]
})

One time query to check if target element is in viewport

import { isInViewport } from 'observe-element-in-viewport'

// element whose visibility we want to track
const target = document.querySelector('.target')

// Using window as viewport, it will check the target element's
// visibility and log true or false.
// This function accepts a second parameter which is the same
// as the first parameter to observeElementInViewport fn i.e.,
// the options object.
console.log(await isInViewport(target))

Demo

Try it here

observe-element-in-viewport's People

Contributors

zeusdeux 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

Watchers

 avatar  avatar  avatar

Forkers

voloshchenkoal

observe-element-in-viewport's Issues

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.