UNPKG

6.79 kBMarkdownView Raw
1# Ks Vue Scrollmagic
2
3[![ks-vue-scrollmagic](https://img.shields.io/npm/v/ks-vue-scrollmagic.svg)](https://www.npmjs.com/package/ks-vue-scrollmagic) [![vue2](https://img.shields.io/badge/vue-2.x-brightgreen.svg)](https://vuejs.org/)
4
5
6Easily use ScrollMagic with vue.js (and nuxt.js...).
7Now production ready!!!
8
9Try it in this [ fiddle ](https://jsfiddle.net/romainPouchol/v9jhjspn/).
10
11---
12
13## Getting started
14
15The 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).
16
17```bash
18 npm i --save ks-vue-scrollmagic
19```
20
21## Usage
22
23##### With Webpack
24
25```js
26import KsVueScrollmagic from 'ks-vue-scrollmagic'
27Vue.use(KsVueScrollmagic)
28```
29##### With Nuxt
30
31Create a ksvuescrollmagic.js file in yur plugins folder, and add it to yout nuxt.config.js file with ssr: false option
32
33ksvuefp.js
34```js
35import KsVueScrollmagic from 'ks-vue-scrollmagic'
36Vue.use(KsVueScrollmagic)
37```
38
39nuxt.config.js
40```js
41{
42 ...
43 plugins: [{
44 src: '~/plugins/ksvuescrollmagic',
45 ssr: false
46 }]
47 ...
48}
49```
50
51##### With a script tag
52
53```html
54<script src="your/assets/folder/ks-vue-scrollmagic/dist/ks-vue-scrollmagic.min.js"></script>
55```
56
57Once installed, the plugin adds $scrollmagic and $gsap to Vue.prototype, to make them easily accessibles in every components.
58
59To use them:
60```js
61TweenMax.to('.whatever', 1, { autoAlpha: 0 }) // won't work
62const scene = new ScrollMagic.Scene() // won't work neither
63
64// Instead, do this:
65
66// To use TweenMax or TimelineMax
67vm.$gsap.TweenMax.to('.whatever', 1, { autoAlpha: 0 }) // Works in every components
68// To use ScrollMagic
69const scene = new vm.$scrollmagic.Scene() // Works in every components
70```
71The communication between components and ScrollMagic controller is done via an event bus, available at vm.$ksvuescr. You can emit and listen to:
72
73Event name | Datas | Description
74----- | ------------- | ---
75addScene | scene name, scene object | Add a new scene to ScrollMagic controller
76destroyScene | scene name | Destroy a specific scene from ScrollMagic controller
77destroy | - | Destroy ScrollMagic and remove it all
78
79```js
80//some examples of using Ks Vue Scrollmagic events
81created () {
82 this.$ksvuescr.$on('addScene', (sceneName, scene) => {
83 console.log(`${sceneName} has been added to controller`)
84 })
85},
86methods: {
87 destroyScrollmagic () {
88 this.$ksvuescr.$emit('destroy') // Destroy the plugin
89 }
90}
91
92```
93
94## Example code
95
96To better understand how the plugin works, let's reproduce the "Wipes panels effect" (from Scrollmagic documentation itself, see original [here](http://scrollmagic.io/examples/advanced/section_wipes_manual.html)) in a vue component.
97
98First of all, let's create our template:
99
100```html
101<template>
102 <div id="app"> // Where our app is mounted
103 <div class="pinContainer" ref="pin"> // The panels wrapper, that has to be pinned during all our animation
104 <section
105 v-for="(p, index) in panels"
106 class="panel"
107 :class="`panel-${index}`"
108 :style="{backgroundColor: p.bgColor}"
109 > // Single panel element, used with with v-for.
110 {{ p.title }}
111 </section>
112 </div>
113 </div>
114</template>
115```
116
117Then, we add a bit of css to style our elements
118```css
119 body {
120 margin: 0;
121 }
122 .pinContainer {
123 width: 100%;
124 height: 100vh;
125 overflow: hidden;
126 position: relative;
127 }
128 .panel {
129 height: 100%;
130 width: 100%;
131 position: absolute;
132 top: 0;
133 left:0;
134 display: flex;
135 justify-content: center;
136 align-items: center;
137 color: white;
138 }
139
140```
141
142Finally we dig into the js part
143
144```js
145export default {
146 mounted () {
147 this.$nextTick(this.pinContainerScene)
148 },
149 data () {
150 return {
151 panels:[
152 {
153 title: 'panel 1',
154 bgColor: '#29b6f6'
155 },
156 {
157 title: 'panel 2',
158 bgColor: '#ef5350'
159 },
160 {
161 title: 'panel 3',
162 bgColor: '#ec407a'
163 },
164 {
165 title: 'panel 4',
166 bgColor: '#66bb6a'
167 }
168 ]
169 }
170 },
171 methods: {
172 pinContainerScene () {
173 const Length = this.panels.length
174
175 // Create a new Timeline (equivalent to new TimelineMax())
176 const tl = new this.$gsap.TimelineMax()
177
178 for (var i = 0; i < Length; i++) { // For each panel in this.panels array:
179 let animFrom, animOutLetters;
180 switch (i) { // Set animFrom value, depending on the index i of the item
181 case 0:
182 break; // First panel is already visible on page load, so no animation
183 case 1:
184 animFrom = {x: '-100%'} // Second panel comes from the left
185 break;
186 case 2:
187 animFrom = {x: '100%'} // Third one comes from the right
188 break;
189 case 3:
190 animFrom = {y: '-100%'} // Finally, the last one comes from the top
191 break;
192 }
193 if (i !== 0) { // For each panel except the one whom index is 0, create the tween and add it to the tl timeline
194 tl.fromTo(`section.panel-${i}`, 1.5, animFrom, {x: '0%', y: '0%', ease: Linear.easeNone})
195 }
196 }
197
198 // create scene and set its params
199 const scene = new this.$scrollmagic.Scene({
200 triggerElement: '.pinContainer',
201 triggerHook: 'onLeave',
202 duration: `${Length * 100}%`
203 })
204 .setPin('.pinContainer')
205 .setTween(tl)
206
207 // Add scene to ScrollMagic controller by emiting an 'addScene' event on vm.$ksvuescr (which is our global event bus)
208 this.$ksvuescr.$emit('addScene', 'pinContainerScene', scene)
209
210 // TAAAAAAADAAAAAAAAAAAA
211 }
212 },
213 destroyed () {
214 // Destroy ScrollMagic when our component is removed from DOM
215 this.$ksvuescr.$emit('destroy')
216 }
217}
218```
219
220You can visualize the result of this example in this[ fiddle ](https://jsfiddle.net/romainPouchol/v9jhjspn/).
221
222## Remaining tasks
223
224- [x] Add installation and utilisation infos to readme.md
225- [ ] Add Iscroll option
226- [ ] Maybe create a directive to automatically destroy scrollmagic's controller
227
228---
229
230## Contribution
231
232If 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 ;)
233
234I'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.
235
236---
237
238## Licence
239
240[ MIT ](http://opensource.org/licenses/MIT)