Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookbook: creating a custom directive for scroll events #1468

Merged
merged 5 commits into from
Mar 9, 2018

Conversation

sdras
Copy link
Member

@sdras sdras commented Mar 6, 2018

This cookbook example shows how you can coordinate animations triggered on scroll with the use of custom directives, and then also how to optimize further and pass arguments for repetitive code.

@sdras sdras requested a review from chrisvfritz March 6, 2018 21:58
@sdras sdras added the cookbook label Mar 6, 2018

## 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 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe is perhaps to use… instead? Unless we're 100% sure that a directive is always shorter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point. I can't think of a time when it wouldn't be shorter but I'm sure there are edge cases out there :)


``` js
Vue.directive('scroll', {
inserted: function(el, binding) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call me nick-picky, but there should be a space after function following to Vue's coding standard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and often we don't use semi-colons ;) (pun not intended).

if (window.scrollY > 50) {
el.setAttribute("style", "opacity: 1; transform: translate3d(0, -10px, 0)")
}
return window.scrollY > 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's an indent issue here.

</div>
```

<p class="tip">Remember! The directive must be registered before the Vue instance</p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe finish the second sentence with a period?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

ease: Sine.easeOut
})
}
return window.scrollY > 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same indent issue I guess.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should just run it through prettier. Too bad the default formatter for codepen is a different style than what we use so I can't use it. Thanks!


## 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 need.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be "fit your needs?"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes :)

@sdras
Copy link
Member Author

sdras commented Mar 9, 2018

Thanks for the review! I updated the PR based on the suggestions. Also included: the ordering of the cookbook was all over the place, so I updated the entries to use solid numbers in the order that the PRs were received to avoid the collision here. I'll start making sections for them in a subsequent PR.

@sdras sdras merged commit f5c487c into vuejs:master Mar 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants