- Adding Instance Properties
- Form Validation
- Editable SVG Icon Systems
- Create a CMS-Powered Blog
- Unit Testing Vue Components
- Creating Custom Scroll Directives
- Debugging in VS Code
- Using Axios to Consume APIs
- Avoiding Memory Leaks
- Client-Side Storage
- Packaging Vue Components for npm
- Dockerize Vue.js App
- Practical use of scoped slots with GoogleMaps
Creating Custom Scroll Directives
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 to create a hook for anything that fires off a particular scroll event.
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:
See the Pen Custom Scroll Directive- CSS Transition by Sarah Drasner (@sdras) on CodePen.
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.
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
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.
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 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, cascading pinning, section wipes, and responsive duration.