Giter Club home page Giter Club logo

vue-waypoint's Introduction

VueWaypoint

trigger functions based on elements' positions, based on viewport

Build Status

Demo

demo page

Installation

npm

$ npm install vue-waypoint --save-dev

Vue

import Vue from 'vue'
import VueWaypoint from 'vue-waypoint'

// Waypoint plugin
Vue.use(VueWaypoint)

Usage

VueWaypoint is a directive named v-waypoint

Template

<template>
  <div v-waypoint="{ active: true, callback: onWaypoint, options: intersectionOptions }"></div>
</template>

Javascript

export default {
  data: () => ({
    intersectionOptions: {
      root: null,
      rootMargin: '0px 0px 0px 0px',
      threshold: [0, 1] // [0.25, 0.75] if you want a 25% offset!
    } // https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
  })
  methods: {
    onWaypoint ({ going, direction }) {
      // going: in, out
      // direction: top, right, bottom, left
      if (going === this.$waypointMap.GOING_IN) {
        console.log('waypoint going in!')
      }

      if (direction === this.$waypointMap.DIRECTION_TOP) {
        console.log('waypoint going top!')
      }
    }
  }
}

API

Directive's options

  • active [boolean]: set this parameter as you wish, changing dynamically the waypoint status (it removes and adds the waypoint physically)

  • callback [function]: every time the waypoint triggers this function will be called with a Waypoint object as parameter

  • options [object]: you can leave this undefined or follow IntersectionObserver API (options)

Waypoint object

Each callback call comes with a Waypoint object defined as follows:

{
  el: Node,
  going: String,
  direction: String,
  _entry: IntersectionObserverEntry
}

You can map going and direction with the following global map, callable in every Vue's Component:

this.$waypointMap

Then you can compare map's elements with the callback's parameters:

if (direction === this.$waypointMap.DIRECTION_TOP) {}

Public API methods

  • VueWaypoint.addObserver (Element el, function callback, Object options)

  • VueWaypoint.removeObserver (Element el, function callback)

  • VueWaypoint.map

Best practices

You are encouraged to use v-waypoint directive since it follows the Vue's flow, anyway you can progammatically add new waypoints as you like, even outside Vue's context.

This can be accomplished with addObserver and removeObserver.

You can call them inside Vue's components with this.$addObserver and this.$removeObserver.

They are also available as standalone-plugin, just go with VueWaypoint.addObserver and VueWaypoint.removeObserver.

Caveats

Waypoint first trigger is on page load, this means it actually triggers its own callback with direction = undefined (yes, we can't determine direction if no scroll has been made by the user)

vue-waypoint's People

Contributors

scaccogatto avatar iamike avatar colloquet avatar blackgearit avatar bralat avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.