0.0.15 • Published 5 years ago

inview v0.0.15

Weekly downloads
315
License
MIT
Repository
github
Last release
5 years ago

inView

Detect when element scrolled to view

License Build Status dependencies Status NPM version

Demo

https://lab.miguelmota.com/inview/demo/

Install

npm install inview

Getting started

Basic example

var inview = InView(el, function(isInView) {
  if (isInView) {
    // do something
    // ...
  }
});

Example showing if visible top half or bottom half of screen

var inview = InView(el, function(isInView, data) {
  if (isInView) {
    if (data.elementOffsetTopInViewHeight < data.inViewHeight/2) {
      console.log('in view (top half)')
    } else {
      console.log('in view (bottom half)')
    }
  } else {
    if (data.windowScrollTop - (data.elementOffsetTop - data.inViewHeight) > data.inViewHeight) {
      console.log('not in view (scroll up)')
    } else {
      console.log('not in view (scroll down)')
    }
  }
})

Destroy InView listeners

var inview = InView(el, function(isInView, data) {
  if (isInView) {
    // do something
    // ...

    this.destroy()
  }
})

// another way
inview.destroy()

Documentation

Constructor:

  • InView(element, callback)

Scroll callback parameters:

  • {boolean} isInView - is in view
  • {object} data - scroll data
  • {number} data.windowScrollTop - scrolled amount
  • {number} data.elementOffsetTop - element top offset
  • {number} data.inViewHeight - height of visible area
  • {number} data.elementOffsetTopInViewHeight - element top offset relative to height of visible area

Visualization:

License

MIT

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

6 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

10 years ago