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

withinviewport

v3.0.1

Published

Determine whether an element is completely within the browser viewport

Downloads

9,442

Readme

Within Viewport

Determine whether elements are completely within the viewport

Includes:

  • The synchronous function, withinViewport()
  • The asynchronous, promise-based function, withinViewportAsync()
  • Optional jQuery plugin with handy selectors and shorthand methods
  • TypeScript support
  • Support for CommonJS and ESM modules

All of the above offer the same features.

Install

yarn add withinviewport

or

npm install withinviewport

And then in your JavaScript or TypeScript:

import { withinViewport /* or withinViewportAsync */ } from 'withinviewport'

Both CommonJS and ESM modules are provided.

Usage

Basic

// Returns true if the element is entirely within view of the window
const elem = document.getElementById('#myElement')

withinViewport(elem) // returns a boolean
withinViewportAsync(elem) // returns a Promise<boolean>

Advanced

All options work the same for both the synchronous and asynchronous functions.

// Test against only some sides of the window for faster performance
withinViewport(elem, { left: 'ignore' })
// Pick another element to act as the viewport (instead of `window`)
withinViewport(elem, { container: document.getElementById('myElem') })
// Define your own viewport crop by specifying thresholds for each side
// Example: element is at least 12px inside the top and right of the viewport
withinViewport(elem, { top: 12, right: 12 })

For more options, see Options section below.

Shorthand notation

// This will only check the bottom and right of the viewport, ignoring the top and left
withinViewport(elem, 'bottom right')
left(elem)

jQuery plugin

Be sure to include the full version of the script as well

<script src="withinviewport.js"></script>
<script src="jquery.js"></script>
<script src="jquery.withinviewport.js"></script>

Basic usage

// Returns true if the element is entirely within the viewport
$('#myElement').is(':within-viewport')
// Returns a jQuery object of all <div>s that are within the viewport
$('div').withinViewport()

Advanced usage

There are shorthand selectors and methods for testing against only one edge of the viewport.

// Returns true if the element is within the left edge of the viewport
// Also works with 'top', 'right', and 'bottom'
$('#myElement').is(':within-viewport-left')
// Returns a jQuery collection of all <div>s within the left edge of the viewport
$('div').withinViewportLeft()
// Same as above, but only elements that are at least 12px inside the left edge
$('div').withinViewportLeft({ left: 12 })

These shortcuts will result in slightly better performance if you're testing hundreds or thousands of elements.

Live updating

If you're looking to keep tabs on elements' whereabouts at all times, you can bind to the window's resize and scroll events. However, for performance reasons, it's strongly recommended to throttle your event listener or use something like James Padolsey's scrollStop event. Firing on every window.scroll event will bring your UI to its knees.

$(window).on('resize scrollStop', _.throttle(function() {
    // Your code here...

    // Example:
    $('div')
        // Momentarily declare all divs out of the viewport...
        .removeClass('within-viewport')
        // Then filter them to reveal which ones are still within it
        .filter(':within-viewport')
            .addClass('within-viewport')
}, 100));

Options

All options work the same across the synchronous and asynchronous functions and the jQuery plugin.

Custom viewport element

If you want to test whether an element is within a scrollable parent element (e.g. which has overflow: auto or scroll), assign the parent element to the container property:

withinViewport(elem, {
    container: document.querySelector('.parent-element')
})

Custom boundaries

For example, a fixed header with a height of 100px that spans the entire width of the page effectively lowers the viewport by 100px from the top edge of the browser window:

withinViewport(elem, { top: 100 })

If you only care about some edges of the viewport, you can specify them to improve performance:

withinViewport(elem, 'left bottom')

You can also combine optins:

withinViewport(elem, { left: 40, container: myDiv })
$('div').withinViewport({ right: -70, top: 'ignore' })

You can specify negative threshold values to allow elements to reside outside the viewport.

Migrating from v2 to v3

The sides option has been deprecated. The following calls are equivalent:

// 2.x: specifying which sides to *monitor*
withinViewport(elem, { sides: ['left', 'right']})

// 3.x: specifying which sides to *ignore*
withinViewport(elem, { top: 'ignore', bottom: 'ignore' })

Browser Support

For the synchronous functions:

  • IE 7(?) and higher
  • All the others except Opera Mini
    • Tested in the latest stable Chrome, Firefox, Safari, and Edge

The asynchronous functions work in any browser that supports promises and IntersectionObserver.

All functions (both versions) are transpiled to ES5.

Credit

Within Viewport is inspired by these similar utilities which only reflect whether an element is at least partially in view:

License

Have fun with it — BSD-3-Clause. See included LICENSE file.

Author

Craig Patik