0.5.2 • Published 6 years ago

vue-on-visible v0.5.2

Weekly downloads
10
License
ISC
Repository
github
Last release
6 years ago

Vue-On-Visible

Easily observe changes in an elements visibility & viewport location.

Designed to help you improve performance and integrate interactivity in response to an elements position on the page.

Example use cases: 1. Trigger an animation on elements entering and exiting the viewport. 2. Lazy loading an image when an element is 10% below the viewport.

See Demo.

This uses the Interaction Observer API and includes a polyfill enabling support in the following browsers:


Table of contents

Installation

npm install --save vue-on-visible

Importing

Install Directive and Components

import Vue from 'vue'
import OnVisible from 'vue-on-visible'

Vue.use(OnVisible)

Directive

Use the v-on-visible directive to integrate visibility updates into your own components.

PropNameTypeDefaultDescription
onUpdateFunctionn/a - REQUIREDFunction to be called when visibility changes occur.
repeatBOOLEANfalseTrigger updates on every enter & exit of viewport?
topOffsetSTRING"0%"When to trigger visibility updates. See Offsets for an explanation.
bottomOffsetSTRING"0%"When to trigger visibility updates. See Offsets for an explanation.

Template:

  <YourComponent v-on-visible="{onUpdate, topOffset, bottomOffset, repeat}" />

Component:

const YourComponent = {
  data() {
    return {
      topOffset: '0%',
      bottomOffset: '0%',
      repeat: true 
    }
  },
  methods: {
    onUpdate(item) {
      // Do something with values...
      // isVisible, isAbove, isBelow, isPartiallyAbove, isPartiallyBelow, intersectionRatio
      // See Properties Section for details
    }
  }
}
  

Component

Using built-in component <OnVisible> with slot-scope for updates on visibility changes.

In this example, Intersection Point is 10% from the top and bottom inside the viewport.

<OnVisible topOffset="-10%" bottomOffset="-10%" :repeat="true">
  <div slot-scope="onVisible">
    <!-- SEE PROPERTIES SECTION BELOW FOR AVAILABLE PROPS -->
    <YourComponent isVisible="onVisible.isVisible" isBelow="onVisible.isBelow"/>
  </div>
</OnVisible>

Properties

The following properties will be send on all visibility updates:

PropNameTypeDescription
isVisibleBOOLEANComponent is in viewport
isAboveBOOLEANComponent is above viewport
isBelowBOOLEANComponent is below viewport
isPartiallyAboveBOOLEANComponent is in viewport, but also above viewport
isPartiallyBelowBOOLEANComponent is in viewport, but also below viewport
intersectionRatioNUMBERAmount of component visible. Decimal between 0 - 1

Offsets

The default offset will trigger visibility changes when your component enters/exits the viewport at the top or bottom. You can change this behavior by providing your own offsets object. Negative offsets are triggered inside of the viewport, while positive offsets are triggered outside the viewport.

For visibility changes to trigger when an element is outside the viewport use a positive number.

// Visibility changes occur 10% above the top of the viewport, and 10% below the bottom of the viewport.
const topOffset = '10%'
const bottomOffset = '10%'

For visibility changes to trigger when an element is inside the viewport use a negative number.

// Visibility changes occur 10% below the top of the viewport, and 10% above the bottom of the viewport.
const topOffset = '-10%'
const bottomOffset = '-10%'
0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago