All Projects → BKWLD → vue-in-viewport-mixin

BKWLD / vue-in-viewport-mixin

Licence: MIT license
Vue 2 mixin to determine when a DOM element is visible in the client window

Programming Languages

javascript
184084 projects - #8 most used programming language
coffeescript
4710 projects
Vue
7211 projects

Projects that are alternatives of or similar to vue-in-viewport-mixin

React Intersection Observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.
Stars: ✭ 2,689 (+2616.16%)
Mutual labels:  scrolling, viewport, intersection-observer
react-awesome-reveal
React components to add reveal animations using the Intersection Observer API and CSS Animations.
Stars: ✭ 564 (+469.7%)
Mutual labels:  viewport, intersection-observer
onscroll-effect
A tiny JavaScript library to enable CSS animations when user scrolls.
Stars: ✭ 35 (-64.65%)
Mutual labels:  scrolling, viewport
React Cool Inview
😎 🖥️ React hook to monitor an element enters or leaves the viewport (or another element).
Stars: ✭ 830 (+738.38%)
Mutual labels:  scrolling, viewport
react-spring-pop
Animate React elements when they enter the viewport with physics based animations
Stars: ✭ 17 (-82.83%)
Mutual labels:  viewport, intersection-observer
svelte-intersection-observer
Detect if an element is in the viewport using the Intersection Observer API
Stars: ✭ 151 (+52.53%)
Mutual labels:  viewport, intersection-observer
react-scroll-trigger
📜 React component that monitors scroll events to trigger callbacks when it enters, exits and progresses through the viewport. All callback include the progress and velocity of the scrolling, in the event you want to manipulate stuff based on those values.
Stars: ✭ 126 (+27.27%)
Mutual labels:  scrolling, viewport
React Intersection Observer
React component for the Intersection <Observer /> API
Stars: ✭ 940 (+849.49%)
Mutual labels:  scrolling, viewport
angular-inviewport
A simple lightweight library for Angular with no other dependencies that detects when an element is within the browser viewport and adds a "sn-viewport-in" or "sn-viewport-out" class to the element
Stars: ✭ 72 (-27.27%)
Mutual labels:  scrolling, viewport
Viewprt
A tiny, dependency-free, high performance viewport position & intersection observation tool
Stars: ✭ 36 (-63.64%)
Mutual labels:  scrolling, viewport
svelte-inview
A Svelte action that monitors an element enters or leaves the viewport.🔥
Stars: ✭ 358 (+261.62%)
Mutual labels:  viewport, intersection-observer
render-props
㸚 Easy-to-use React state containers which utilize the render props (function as child) pattern
Stars: ✭ 33 (-66.67%)
Mutual labels:  scrolling, viewport
vue-observable
IntersectionObserver, MutationObserver and PerformanceObserver in Vue.js
Stars: ✭ 24 (-75.76%)
Mutual labels:  viewport, intersection-observer
monolazy
Extended nanocomponent providing onEnter callback
Stars: ✭ 21 (-78.79%)
Mutual labels:  intersection-observer
jquery-scrollwatch
jQuery plugin for determining active sections on the page based on scrolling
Stars: ✭ 18 (-81.82%)
Mutual labels:  scrolling
NoobScroll
A lightweight jQuery Plugin that add some cool function to make scrolling more fun [Archive]
Stars: ✭ 43 (-56.57%)
Mutual labels:  scrolling
mixin bot
A simple API wrapper for Mixin Network in Ruby
Stars: ✭ 12 (-87.88%)
Mutual labels:  mixin
react-smart-scroll
Efficient rendering of long lists in React
Stars: ✭ 27 (-72.73%)
Mutual labels:  scrolling
taeseung vimrc
Taeseung Lee's vim setting
Stars: ✭ 16 (-83.84%)
Mutual labels:  viewport
nuxt-viewport
🌈 Define custom viewports for your Nuxt project
Stars: ✭ 55 (-44.44%)
Mutual labels:  viewport

Vue In Viewport Mixin

Vue 3 mixin to determine when a DOM element is visible in the client window by updating Vue data values using the IntersectionObserver. You can then use those data values to do things like trigger transitions.

You may want to check out the directive vesion of this package: vue-in-viewport-directive.

Demo: https://bkwld.github.io/vue-in-viewport-mixin

Install

This package depends on the IntersectionObserver API which needs a polyfill for old browsers, including all of IE. Consider using the W3C IntersectionObserver polyfill.

Usage

  • Just require the mixin from your component and use it with watch:

     inViewport = require('vue-in-viewport-mixin');
     module.exports = {
     	mixins: [ inViewport ],
     	watch: {
     		'inViewport.now': function(visible) {
     			console.log('This component is '+( visible ? 'in-viewport' : 'hidden'));
     		}
     	}
     }
  • Use the optional offset props to configure the component:

     <large-copy
     	:in-viewport-root-margin='-50% 0%'
     	:in-viewport-once='true'>
     </large-copy>
  • Use data values within your component to trigger transitions (or do other things):

     <div class='.large-copy'>
     	<transition name='fade'>
     		<h1 v-show='inViewport.now'>{{ copy.title }}</h1>
     	</transition>
     </div>
  • It's worth nothing that the IntersectionObserver counts an element as intersecting the viewport when it's top offset is equal to the height of height of the page. For instance, if you have an element with margin-top: 100vh, that actually counts as intersecting and this mixin will have inViewport.now == true.

  • If your component's height changes, you should call this.reInitInViewportMixin() to recreate the IntersectionObserver instance with the new, calculated threshold.

Props

  • in-viewport-active (true) - Whether to listen update the mixin data. Setting to false later in the lifecyle will remove listeners and setting back to true will re-apply listeners.

  • in-viewport-once (false) - Whether to remove listeners once the component enters viewport. If the component is in viewport when mounted, listeners are never added.

  • in-viewport-root-margin (0px 0px -1px 0px) - Specify the IntersectionObserver rootMargin. For example, set to "-20% 0%" to delay the inViewport.now from switching state until your component is 20% of the viewport height into the page. This defaults to 0px 0px -1px 0px so that a target that is positioned at 100vh registers as not in viewport.

  • in-viewport-root - Specify the IntersectionObserver root. Defaults to the browser viewport.

  • in-viewport-threshold ([0,1]) - Specify the IntersectionObserver threshold. The defaults should work for most cases.

Data

The whole point of this package is for you to make use of this data to do stuff. The following describes the data that is mixed into your component. Note that all properties are namespaced under inViewport.

data: {
	inViewport: {
		now: Boolean   // Is some part of the component currently in the viewport?
		fully: Boolean // Is the component completely in the viewport?
		above: Boolean // Is any part of the component above the viewport?
		below: Boolean // Is any part of the component below the viewport?
	}
}

Tests

  1. Start Storybook: yarn storybook
  2. Open Cypress: yarn cypress open

The Travis tests that run on deploy run against the demo site which gets updated as part of the npm version commands.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].