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

onscreenness

v1.5.0

Published

Supply visibility classes and data to selected elements on your webpage.

Downloads

25

Readme

Build Status Coverage Status DeepScan grade

onscreenness

Sign up your elements and a have their onscreen presence be assessed. With the html-classes and data offered here, your elements can make an grand entrance on the viewport!

Sign up your elements

Once bundled in your project onScreenness is available as a global object. You can sign up your elements for the onScreenness-treat using a CSS selector:

let querySelector = '#myElement'
onScreenness.collect(querySelector);
onScreenness.collect('.paragraph');

The collect method may be called several times to build up the collection.

Elements covered by a query in the collection can be blacklisted with an alternative query:

onScreenness.exclude('.paragraph.emphasis');
onScreenness.exclude('#that-paragraph');

CSS selectors added to the collection can be removed:

onScreenness.remove('#myElement');

All specified queries can be removed:

onScreenness.reset();

Automatic pick-up

Onscreenness will also watch the html for elements with a data attribute:

<section data-onscreenness>...</section>

In this case no further scripting is needed. Blacklisting will also work on automatically picked up elements. Resetting will work on such element when the collection contains a query capable of selecting it.

Have the onscreen presence data

When the visible part of the webpage changes, the onscreenness of the collected elements is assesed.

Depending on the place the element has relative to the viewport, the element is assigned a class 'onscreen', 'crossscreen' or 'offscreen'.

onscreenness classes

As some elements are too big to be assigned the 'onscreen' class, the 'overscreen' class was created. The class 'overscreen' is assigned in two cases:

  • The element covers the viewport in one aspect and is entirely visible in the other aspect.
  • The element covers the viewport entirely.

Class 'overscreen' appears together with 'crossscreen', except when it covers the viewport exactly.

Apart from the classes that describe the state, there are two classes 'nearingscreen' and 'leavingscreen'. With these you know what to expect later on.

Styling and scripting

With stylerules you can now set the elements' appearance while it moves through the viewport, that's up to you.

Also, each involved element gets an 'onscreenness' and a 'overlapping' data attribute. Onscreenness is the extent to which the element is inside the viewport. Overlapping is the extent to which the element fills the viewport. You could script on it.

The onscreenness properties are passed as the arguments:

let myFunction = props => { 
    if ( props.surfacePresence > .75) {
        console.log('myElement has been visible to the user')
    }
}
onScreenness.collect('#myElement', myFunction);

When using a traditional function, the current element is exposed as 'this':

let myFunction = function ( props ) { 
    if ( Number ( this.dataset['onscreenness'] ) > .75) {
        console.log(`${this.id} has been visible to the user`)
    }
}
onScreenness.collect('#myElement', myFunction);

The function can be run when the classname changes:

let myFunction = function ( props ) { 
    if ( props.addClass.indexOf( 'onscreen' ) > -1 ) {
        console.log(`${this.id} has been fully visible to the user`)
    }
}
onScreenness.collect('#myElement', myFunction);

see demo folder

...

Install for vanilla javascript-app

Place the following code in the head-section of your webpage:

<script src="https://unpkg.com/onscreenness@latest"></script>

Triggers for assesment of presence

  • readystatechange to interactive
  • resize window
  • scrolling
  • document changes

Application Programming Interface

see API description

Version History

see changelog