Ks Vue Scrollmagic
Easily use ScrollMagic with vue.js (and nuxt.js...). Now production ready!!!
Try it in this fiddle .
Getting started
The plugin only works with 2nd version of Vue.js. You don't need to include scrollmagic.js nor GSAP in your bundle or html, as they're included in the package (for the moment, I'll maybe remove gsap and create a new plugin for it in the future).
npm i --save ks-vue-scrollmagic
Usage
With Webpack
Vue
With Nuxt
Create a ksvuescrollmagic.js file in yur plugins folder, and add it to yout nuxt.config.js file with ssr: false option
ksvuefp.js
Vue
nuxt.config.js
... plugins: src: '~/plugins/ksvuescrollmagic' ssr: false ...
With a script tag
Once installed, the plugin adds $scrollmagic and $gsap to Vue.prototype, to make them easily accessibles in every components.
To use them:
TweenMax // won't workconst scene = // won't work neither // Instead, do this: // To use TweenMax or TimelineMaxvm$gsapTweenMax // Works in every components// To use ScrollMagicconst scene = // Works in every components
The communication between components and ScrollMagic controller is done via an event bus, available at vm.$ksvuescr. You can emit and listen to:
Event name | Datas | Description |
---|---|---|
addScene | scene name, scene object | Add a new scene to ScrollMagic controller |
destroyScene | scene name | Destroy a specific scene from ScrollMagic controller |
destroy | - | Destroy ScrollMagic and remove it all |
//some examples of using Ks Vue Scrollmagic events { this$ksvuescr}methods: { this$ksvuescr // Destroy the plugin }
Example code
To better understand how the plugin works, let's reproduce the "Wipes panels effect" (from Scrollmagic documentation itself, see original here) in a vue component.
First of all, let's create our template:
// Where our app is mounted // The panels wrapper, that has to be pinned during all our animation // Single panel element, used with with v-for. {{ p.title }}
Then, we add a bit of css to style our elements
Finally we dig into the js part
{ this } { return panels: title: 'panel 1' bgColor: '#29b6f6' title: 'panel 2' bgColor: '#ef5350' title: 'panel 3' bgColor: '#ec407a' title: 'panel 4' bgColor: '#66bb6a' } methods: { const Length = thispanelslength // Create a new Timeline (equivalent to new TimelineMax()) const tl = for var i = 0; i < Length; i++ // For each panel in this.panels array: let animFrom animOutLetters; if i !== 0 // For each panel except the one whom index is 0, create the tween and add it to the tl timeline tl // create scene and set its params const scene = triggerElement: '.pinContainer' triggerHook: 'onLeave' duration: `%` // Add scene to ScrollMagic controller by emiting an 'addScene' event on vm.$ksvuescr (which is our global event bus) this$ksvuescr // TAAAAAAADAAAAAAAAAAAA } { // Destroy ScrollMagic when our component is removed from DOM this$ksvuescr }
You can visualize the result of this example in this fiddle .
Remaining tasks
- Add installation and utilisation infos to readme.md
- Add Iscroll option
- Maybe create a directive to automatically destroy scrollmagic's controller
Contribution
If your facing difficulties to use it, find some bugs or unexpected behaviour... feel free to open a new issue, I'll try to answer you asap ;)
I'm just a lowly frontend developer trying to master ES6, so suggestions are more than welcome, not only for feature requests but also for coding style improvements.