From f5c487c039c6a08618265bb2b421d5d287e0cd7f Mon Sep 17 00:00:00 2001 From: Sarah Drasner Date: Fri, 9 Mar 2018 12:12:11 +0000 Subject: [PATCH] Cookbook: creating a custom directive for scroll events (#1468) * start work on cookbook scroll article * put in the example, keep writing * light edits to text * tiny tweaks to text * small edits based on PR review, and fix ordering to solid numbers that are consistent --- src/v2/cookbook/adding-instance-properties.md | 2 +- .../creating-custom-scroll-directives.md | 154 ++++++++++++++++++ src/v2/cookbook/editable-svg-icons.md | 2 +- src/v2/cookbook/form-validation.md | 2 +- .../cookbook/unit-testing-vue-components.md | 2 +- 5 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 src/v2/cookbook/creating-custom-scroll-directives.md diff --git a/src/v2/cookbook/adding-instance-properties.md b/src/v2/cookbook/adding-instance-properties.md index 6c67dc369b..100758a9b1 100644 --- a/src/v2/cookbook/adding-instance-properties.md +++ b/src/v2/cookbook/adding-instance-properties.md @@ -1,7 +1,7 @@ --- title: Adding Instance Properties type: cookbook -order: 1.3 +order: 2 --- ## Simple Example diff --git a/src/v2/cookbook/creating-custom-scroll-directives.md b/src/v2/cookbook/creating-custom-scroll-directives.md new file mode 100644 index 0000000000..a432cbfe13 --- /dev/null +++ b/src/v2/cookbook/creating-custom-scroll-directives.md @@ -0,0 +1,154 @@ +--- +title: Creating Custom Scroll Directives +type: cookbook +order: 7 +--- + +## Simple Example + +There are many times that we might want to add a bit of behavior, especially animation, to a scroll event on a site. There are many ways to do so, but the path with the least amount of code and dependencies is perhaps to use a [custom directive](https://vuejs.org/v2/guide/custom-directive.html) to create a hook for anything that fires off a particular scroll event. + +```js +Vue.directive('scroll', { + inserted: function (el, binding) { + let f = function (evt) { + if (binding.value(evt, el)) { + window.removeEventListener('scroll', f) + } + } + window.addEventListener('scroll', f) + } +}) + +// main app +new Vue({ + el: '#app', + methods: { + handleScroll: function (evt, el) { + if (window.scrollY > 50) { + el.setAttribute( + 'style', + 'opacity: 1; transform: translate3d(0, -10px, 0)' + ) + } + return window.scrollY > 100 + } + } +}) +``` + +```html +
+

Scroll me

+
+

Lorem ipsum dolor sit amet, consectetur adipisicing elit. A atque amet harum aut ab veritatis earum porro praesentium ut corporis. Quasi provident dolorem officia iure fugiat, eius mollitia sequi quisquam.

+
+
+``` + +

Remember! The directive must be registered before the Vue instance.

+ +We'd also need a style property that will transition the intermediary values here, in this case: + +``` +.box { + transition: 1.5s all cubic-bezier(0.39, 0.575, 0.565, 1); +} +``` + +

See the Pen Custom Scroll Directive- CSS Transition by Sarah Drasner (@sdras) on CodePen.

+ + +Or, with GreenSock(GSAP) or any other JavaScript animation library, the code becomes even more simple: + +```js +Vue.directive('scroll', { + inserted: function (el, binding) { + let f = function (evt) { + if (binding.value(evt, el)) { + window.removeEventListener('scroll', f) + } + } + window.addEventListener('scroll', f) + } +}) + +// main app +new Vue({ + el: '#app', + methods: { + handleScroll: function (evt, el) { + if (window.scrollY > 50) { + TweenMax.to(el, 1.5, { + y: -10, + opacity: 1, + ease: Sine.easeOut + }) + } + return window.scrollY > 100 + } + } +}) +``` + +Though we would remove the previous CSS transition from this implementation because it's now handled with JavaScript. + +## The Benefit of Using Custom Directives + +Vue is rich with options for directives, most of which cover very common use-cases, which can create a very productive developer experience. But even if you have an edge case not covered by the framework, it's got you covered in this case as well, because you can quite easily create a custom directive to fit your needs. + +Attaching and removing scroll events to elements is a really good use case for this technique because just like other directives we use, they are necessarily tied to the element and otherwise, we'd have to find the reference for it in the DOM. This pattern avoids the need for traversal, and keeps the event logic paired with the node that it's in reference to. + +## Real-World Example: Using a Custom Scroll Directive for Cascading Animations + +In the course of creating a cohesive site, you may find that you're reusing the same type of animation logic in several areas. It seems simple, we would then create a very specific custom directive, right? Well, typically if you're reusing it, you will need to change it _just_ slightly for each use. + +To help keep our code concise and legible, we would want to pass in some predefined arguments such as the beginning point and ending of the animation as we scroll down the page. + +**This example is better viewed in the [full screen version](https://s.codepen.io/sdras/debug/078c19f5b3ed7f7d28584da450296cd0).** + +

See the Pen Scrolling Example- Using Custom Directives in Vue by Sarah Drasner (@sdras) on CodePen.

+ + +In the demo above, each of the sections has two different types of animation triggered from the scroll: a morphing animation, and a drawing animation that animates the individual paths in the SVG. We reuse those two animations, so we can create a custom directive for each. The arguments we'll pass in will help keep everything simple and reusable. + +To show how we do this, we'll take a look at the morphing shape example, where we'll need to state the start and finish, as well as pass in a path value that we'll create a morph to. These arguments are each defined as `binding.value.foo`: + +```js +Vue.directive('clipscroll', { + inserted: function (el, binding) { + let f = function (evt) { + var hasRun = false + if (!hasRun && window.scrollY > binding.value.start) { + hasRun = true + TweenMax.to(el, 2, { + morphSVG: binding.value.toPath, + ease: Sine.easeIn + }) + } + if (window.scrollY > binding.value.end) { + window.removeEventListener('scroll', f) + } + } + window.addEventListener('scroll', f) + } +}) +``` + +We can then use this animation in our template, in this case we're attaching the directive to the `clipPath` element, and pass all of our arguments to the directives in an object. + +```html + + + +``` + +## Alternative Patterns + +Custom directives are extremely useful, but you may find some situations where you need something very specific that already exists in scrolling libraries that you don't wish to rebuild from scratch yourself. + +[Scrollmagic](http://scrollmagic.io/) has a very rich ecosystem of offerings to work with, as well as good documentation and demos to explore. This includes, but is not limited to things like [parallax](http://scrollmagic.io/examples/advanced/parallax_scrolling.html), [cascading pinning](http://scrollmagic.io/examples/expert/cascading_pins.html), [section wipes](http://scrollmagic.io/examples/basic/section_wipes_natural.html), and [responsive duration](http://scrollmagic.io/examples/basic/responsive_duration.html). diff --git a/src/v2/cookbook/editable-svg-icons.md b/src/v2/cookbook/editable-svg-icons.md index e1cbd42926..b2e2ab709b 100644 --- a/src/v2/cookbook/editable-svg-icons.md +++ b/src/v2/cookbook/editable-svg-icons.md @@ -1,7 +1,7 @@ --- title: Editable SVG Icon Systems type: cookbook -order: 2 +order: 4 --- ## Base Example diff --git a/src/v2/cookbook/form-validation.md b/src/v2/cookbook/form-validation.md index 966ef656cb..589a4e312e 100644 --- a/src/v2/cookbook/form-validation.md +++ b/src/v2/cookbook/form-validation.md @@ -1,7 +1,7 @@ --- title: Form Validation type: cookbook -order: 1.2 +order: 3 --- ## Simple Example diff --git a/src/v2/cookbook/unit-testing-vue-components.md b/src/v2/cookbook/unit-testing-vue-components.md index 2d219b3c64..c12305bd6f 100644 --- a/src/v2/cookbook/unit-testing-vue-components.md +++ b/src/v2/cookbook/unit-testing-vue-components.md @@ -1,7 +1,7 @@ --- title: Unit Testing Vue Components type: cookbook -order: 1.4 +order: 6 --- ## Simple Example