fires a @collided[-groupName] event on collision with viewport or any other bounding box, Waypoints-like
- Uses requestAnimationFrame
- Wrap up your components in groups
- Group all your components in the
window
group by default - Checks collisions group by group and fire custom events when a collision happens
$ npm install vue-collision --save-dev
import VueCollision from 'vue-collision'
// collision
Vue.use(VueCollision, { globalTriggers: ['resize', 'scroll'] })
- options [optional]: object defining what triggers the groups' checks
<template>
<component-name v-collision="['groupone']" @collide="onCollideHandler" @non-collide="nonCollideHandler" @collide-groupone="onGroupOneCollide" @non-collide-groupone="nonGroupOneCollide"></component-name>
</template>
<script>
export default {
methods: {
onCollideHandler (collider) {
// logic for 'window' group, called when the component collides with window
},
nonCollideHandler (collider) {
// logic for 'window' group, called when the component does NOT collide with window
},
onGroupOneCollide (collider) {
// logic for 'groupone' group, called when the component collides inside 'groupone' group
},
nonGroupOneCollide (collider) {
// logic for 'groupone' group, called when the component does NOT collide inside 'groupone' group
}
}
}
</script>
- Add
v-collision
directive to any component to make it part of thevue-collision
family and awindow-group
's direct son - Specify a
v-collision.prevent
modifier in order to exclude the component fromwindow-group
- Add a value to the directive (
v-collision="['groupone', 'grouptwo']"
) in order to reference the component into thegroupone
andgrouptwo
groups - You can mix it together:
v-collision.prevent="['groupone', 'grouptwo']"
@collide
: happens when the component is colliding with the window (based on: innerWidth and innerHeight)@non-collide
: happens when the component is not colliding with the window@collide[-groupName]
: happens when the component is colliding with someone in the same group@non-collide[-groupName]
: happens when the component is not colliding with something in the same group (can collide with any other component in the same group at the same time)- Every event calls a
function (collider)
when fired. Thecollider
is the Vue istance that is colliding withthis
VueCollision.checkAllGroups()
: it checks all the groups (even window's one) and fire the events stackVueCollision.checkGroups(Array windowTest, Object customGroups)
: it tests the passed groups and/or Vue's components instances.windowTest
must be an array containing ComponentscustomGroups
must be and an Object defined as:
customGroups = {
groupName: {
combinations: [
[Component, Component],
[Component, Component],
...
]
},
groupTwoName: {
combinations: [
[Component, Component],
[Component, Component],
...
]
}
...
}
This software uses mocha as testing framework, some functions are not being fully tested (checkGroups
and install
) since creating a fake VueJS environment in order to test some functions that urely on already fully tested sub-functions seems not worth.
- Clone this repository
cd vue-viewports
npm install
npm test
Feel free to contribute and ask questions