11# ScrollTrigger
22[ ![ License] ( http://img.shields.io/:license-mit-blue.svg )] ( https://github.com/terwanerik/ScrollTrigger/blob/master/LICENSE )
33[ ![ Issues] ( https://img.shields.io/github/issues/terwanerik/ScrollTrigger.svg )] ( https://github.com/terwanerik/ScrollTrigger/issues )
4- ![ GitHub release] ( https://img.shields.io/github/release/terwanerik/ScrollTrigger.svg )
5- ![ npm (scoped)] ( https://img.shields.io/npm/v/@terwanerik/scrolltrigger )
4+ [ ![ GitHub release] ( https://img.shields.io/github/release/terwanerik/ScrollTrigger.svg )] ( https://github.com/terwanerik/ScrollTrigger/releases )
5+ [ ![ npm (scoped)] ( https://img.shields.io/npm/v/@terwanerik/scrolltrigger )] ( https://www.npmjs.com/package /@terwanerik/scrolltrigger)
66
77Let your page react to scroll changes.
88
@@ -23,8 +23,11 @@ import ScrollTrigger from '@terwanerik/scrolltrigger'
2323const trigger = new ScrollTrigger () // When not using npm, create a new instance with 'new ScrollTrigger.default()'
2424// Add all html elements with attribute data-trigger
2525trigger .add (' [data-trigger]' )
26+ ```
27+
28+ Now in your CSS add the following classes, this fades the ` [data-trigger] ` elements in and out.
2629
27- // Now in your CSS add the following classes, this fades the [data-trigger] elements in and out
30+ ``` css
2831.visible , .invisible {
2932 opacity : 0.0 ;
3033 transition : opacity 0.5s ease ;
@@ -239,6 +242,103 @@ receivedTrigger.once
239242receivedTrigger .visible
240243```
241244
245+ ## Migrating from 0.x to 1.x
246+ The main differences between ` 0.x ` and ` 1.x ` are the way you add and configure your
247+ triggers. ` 0.x ` added all HTMLElement's with the data-scroll attribute by default,
248+ ` 1.x ` doesn't do that, this requires you to add the triggers yourself. This improves
249+ the configuration of the triggers.
250+
251+ Also, please note that when * not* using a package manager / webpack, and you're
252+ just importing the minified version, you'll have to always use ` new ScrollTrigger.default() ` .
253+
254+ ``` html
255+ <script src =" dist/ScrollTrigger.min.js" ></script >
256+ <script >
257+ var trigger = new ScrollTrigger.default ()
258+ </script >
259+ ```
260+
261+ Take for example the following element in ScrollTrigger ` 0.x ` :
262+
263+ ``` html
264+ <div data-scroll =" once addHeight" data-scroll-showCallback =" alert('Visible')" data-scroll-hideCallback =" alert('Invisible')" ></div >
265+ ```
266+
267+ In ScrollTrigger ` 1.x ` you would write this mostly in JavaScript:
268+
269+ ``` javascript
270+ // Say you have some divs with class 'animateMe'
271+ const scrollTrigger = new ScrollTrigger ()
272+ scrollTrigger .add (' .animateMe' , {
273+ once: true , // same functionality as the `once` flag in v0.x
274+ offset: {
275+ element: {
276+ y: 1.0 // note that we pass a float instead of an integer, when the
277+ // offset is a float, it takes it as a percentage, in this
278+ // case, add 100% of the height of the element, the same
279+ // functionality as the `addHeight` flag in v0.x
280+ }
281+ },
282+ toggle: {
283+ callback: {
284+ in : () => { // same as the data-scroll-showCallback, no need to set a
285+ // custom callScope when calling custom functions and
286+ // the possibility to return a Promise
287+ alert (' Visible' )
288+ },
289+ out : () => { // same as the data-scroll-hideCallback
290+ alert (' Invisible' )
291+ }
292+ }
293+ }
294+ })
295+ ```
296+
297+ The advantage of writing all this in javascript is the configuration possible, say
298+ i want to change the offset of the element after the first time it's been visible
299+ (e.g. remove the ` addHeight ` flag after it's been shown):
300+
301+ ``` javascript
302+ scrollTrigger .add (' .animateMe' , {
303+ offset: {
304+ element: {
305+ y: 1.0
306+ }
307+ },
308+ toggle: {
309+ callback: {
310+ in : (trigger ) => {
311+ // remove the element offset
312+ trigger .offset .element .y = 0
313+ }
314+ }
315+ }
316+ })
317+ ```
318+
319+ Another example for setting custom classes per toggle;
320+
321+ ``` html
322+ <div data-scroll =" toggle(animateIn, animateOut)" ></div >
323+ ```
324+
325+ Becomes
326+
327+ ``` javascript
328+ const scrollTrigger = new ScrollTrigger ()
329+
330+ scrollTrigger .add (' [data-scroll]' , {
331+ toggle: {
332+ class: {
333+ in: ' animateIn' ,
334+ out: ' animateOut'
335+ }
336+ }
337+ })
338+ ```
339+
340+ If you have any questions on migrating to ` v1.x ` feel free to [ create a new issue] ( https://github.com/terwanerik/ScrollTrigger/issues ) .
341+
242342## Contributing
243343Fork, have a look in the ` src/ ` directory and enjoy! If you have improvements, start a new branch & create a pull request when you're all done :)
244344
0 commit comments