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

observe-element-in-viewport

v0.0.15

Published

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

Downloads

64,397

Readme

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

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