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

@horaceshmorace/nowyouseeme

v1.1.2

Published

A Javascript utility for watching for changes to the viewability of one or more HTML elements within a given viewport.

Downloads

5

Readme

nowyouseeme

A simple vanilla JavaScript utility that watches for changes in the viewability of a specified list of elements, at a set interval, in a given viewport (i.e., another HTML element, which itself must be viewable within the visible bounds of the current window).

Rather than responding directly to browser events, nowyouseeme uses a setInterval timer, which is significantly more performant.

Usage

// Create a watcher
const watcher = nowyouseeme(100)

// Trigger viewability checks when the window scrolls
window.addEventListener('scroll', watcher.check)

// Track a specific HTML element
watcher.track(options)

// Start the watcher
watcher.watch()

API

To create a watcher, call nowyouseeme(), optionally passing the number of milliseconds between each interval (the default is 200).

const watcher = nowyouseeme(100)

The returned watcher is an API of functions for configuring and controlling viewability tracking on specified HTML elements.

| function | description | |----------|-------------| | [watcher].track(options) | Configures viewability tracking for a single HTML element. | | [watcher].check() | Sets an internal flag that informs the interval timer that a viewability change might have occured. Intended primarily to be used as a callback for window's scroll, resize, and load events. | | [watcher].watch() | Starts the interval timer to watch for changes to the viewability of the specified HTML elements. | | [watcher].stop() | Stops the interval timer so that changes to the viewability of the specified HTML elements are no longer watched. Ideal for when you've been tracking a DOM element rendered by a React component, but now the component is unmounting. |

[watcher].track Options

An object with properties to configure viewability tracking for a single HTML element.

| property | type | default | description | |----------|------|---------|-------------| | element | String or HTMLElement | None. Required. | A CSS selector, or a reference to an HTMLElement object. | | viewport | String or HTMLElement | document.body | A CSS selector, or a reference to an HTMLElement object. | | onViewable | Function | None. Required. | a function to call after the element becomes viewable | | onNotViewable | Function | noop | a function to call after the element becomes not viewable | | percentage | Number | 50 | a numeric value from 1-100 indicating the minimum visible area of the element to consider viewable. |

Use Case

Trigger a side effect once an element is in view, and another when it is not in view. The demonstrated side effects inject text into the tracked element. Another example would be to inject an ad into an element only once the element is viewable.

<script>
const watcher = nowyouseeme(100)

window.addEventListener('load', watcher.check)
window.addEventListener('scroll', watcher.check)
window.addEventListener('resize', watcher.check)

watcher.track({
  element: '#trackme',
  viewport: '#container,
  onViewable: element => { element.innerText = 'VIEWABLE!' },
  onNotViewable: element => { element.innerText = 'not viewable until 75% visible' },
  percentage: 75
})

watcher.watch()
</script>
...
<div id="container">
  <div id="trackme" />
</div>