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

verge

v1.10.2

Published

viewport utilities module

Downloads

10,922

Readme

verge

verge is a compact set of cross-browser viewport utilities written in native JavaScript. It includes the ability to detect if an element is in the current viewport. It works as a standalone module, an ender module, or as a jQuery plugin. (npm: verge)

API

  • .viewportW()
  • .viewportH()
  • .viewport() 1.8+
  • .inViewport()
  • .inX()
  • .inY()
  • .scrollX()
  • .scrollY()
  • .mq() 1.6+
  • .rectangle()
  • .aspect() 1.7+

Accuracy

  • verge's viewport methods represent the CSS viewport and thus match @media (width) and @media (height) breakpoints.
  • At certain zooms the viewport dimensions given by verge may be 1px off the expected @media value due to native rounding. For greater precision (at a slight speed tradeoff) consider actual.

.viewportW()

verge.viewportW() // -> viewport width in pixels

.viewportH()

verge.viewportH() // -> viewport height in pixels

.viewport()

Get both CSS viewport dimensions as an object with width and height properties.

verge.viewportW() === verge.viewport().width  // always true
verge.viewportH() === verge.viewport().height // always true

The .viewportW() syntax is slightly faster.

.inViewport()

Test if any part of an element (or the first element in a matched set) is in the current viewport. Returns boolean.

verge.inViewport(elem) // true if elem is in the current viewport
verge.inViewport(elem, 100) // true if elem is in the current viewport or within 100px of it
verge.inViewport(elem, -100) // true if elem is in the current viewport and not within 99px of the edge

Performance tip for sites that only scroll along 1 axis

verge.inViewport(elem) === verge.inX(elem) && verge.inY(elem) // always true
Substitute inViewport with: inY on vertical sites, inX on horizontal ones.
  • On pages without horizontal scroll, inX is always true.
  • On pages without vertical scroll, inY is always true.
  • If the viewport width is >= the document width, then inX is always true.

.inX()

Test if any part of an element (or the first element in a matched set) is in the same x-axis section as the viewport. Returns boolean.

verge.inX(elem) // true if elem is in same x-axis as the viewport (exact)
verge.inX(elem, 100) // true if elem is in same x-axis as the viewport or within 100px of it
verge.inX(elem, -100) // true if elem in is the viewport and not within 99px of the edge

.inY()

Test if any part of an element (or the first element in a matched set) is in the same y-axis section as the viewport. Returns boolean.

verge.inY(elem) // true if elem is in same y-axis as the viewport (exact)
verge.inY(elem, 100) // true if elem is in same y-axis as the viewport or within 100px of it
verge.inY(elem, -100) // true if elem in is the viewport and not within 99px of the edge

.scrollX()

Get the horizontal scroll position in pixels. (Like window.scrollX, but cross-browser.)

verge.scrollX() // -> horizontal pixels scrolled

.scrollY()

Get the vertical scroll position in pixels. (Like window.scrollY, but cross-browser.)

verge.scrollY() // -> vertical pixels scrolled

.mq()

.mq(mediaQueryString)

Test if a media query is active.

verge.mq('(min-color:2)') // -> boolean
verge.mq('tv') // -> boolean

.rectangle()

.rectangle(element, cushion?)

Get an a object containing the properties top, bottom, left, right, width, and height with respect to the top-left corner of the current viewport, and with an optional cushion amount. Its return is like that of the native getBoundingClientRect.

The optional cushion parameter is an amount of pixels to act as a cushion around the element. If none is provided then it defaults to 0 and the rectangle will match the native rectangle. If a cushion is specified, the properties are adjusted according to the cushion amount. If the cushion is positive the rectangle will represent an area that is larger that the actual element. If the cushion is negative then the rectangle will represent an area that is smaller that the actual element.

verge.rectangle(element) // rectangle object
verge.rectangle(element, 100) // rectangle object adjusted by 100 pixels

.aspect()

.aspect(object?)

Get the aspect ratio of the viewport or of an object with width/height properties.

verge.aspect() // -> viewport aspect ratio
verge.aspect(element) // -> element aspect ratio
verge.aspect(screen) // -> device aspect ratio
1 < verge.aspect() // => landscape orientation

Integrate

jQuery

jQuery.extend(verge)

ender

ender build verge

Contribute

Contribute by making edits in ./src or reporting issues.

npm install
npm test