New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

dom-oninviewport

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dom-oninviewport

require('dom-oninviewport')(elm, callback)

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

onInViewport

Know when a DOM element is inside the viewport. You can use this for lazily loading images, infinite scroll, user tracking, etc.

Sports an UMD definition.

Usage

Call onInViewport with an element and a callback. The callback gets called (once and only once) when the element is in the viewport.

If the element is already in the viewport, onInViewport is called immediately.

    onInViewport(document.getElementById('myElement'), function () {
        // Infinite scroll, lazy load, whatever!
    })

To check if an element is in the viewport, use onInViewport.isIn.

    onInViewport.isIn(myElement);  // -> true if it's inside the viewport, false otherwise.

Advanced stuff

Faking a user scroll

You can force onInViewport to reconsider things. If you have something which may push new things onto the screen without the user actually scrolling (such as a slider), you may want to tell onInViewport that the scroll changed, so it can recalculate whether the elements are on the screen or not.

To fake a user-initiated scroll, call onInViewport.onScroll. This is throttled (because browsers send a lot of scroll and resize events), so don't worry about calling it lots of times.

configure what is considered "in viewport"

onInViewport.isIn = function(elm) { return Boolean }

The default implementation is as follows:

onInViewport.isIn = function isIn(elm) {
    elm = elm.getBoundingClientRect()
    return elm.right > 0
        && elm.bottom > 0
        && elm.top < document.documentElement.clientHeight
        && elm.left < document.documentElement.clientWidth
}

Copy and paste this to your code and add some conditions so that onInViewport knows how to deal with your sliders, modals, maps and whatever.

FAQs

Package last updated on 16 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc