You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/v2/cookbook/creating-custom-scroll-directives.md
+20-20
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
-
title: Creating Custom Scroll Directives
2
+
title: 创建自定义滚动指令
3
3
type: cookbook
4
4
order: 7
5
5
---
6
6
7
-
## Simple Example
7
+
## 简单的示例
8
8
9
-
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.
<pclass="tip">Remember! The directive must be registered before the Vue instance.</p>
49
+
<pclass="tip">记住!指令必须在 Vue 实例之前注册好。</p>
50
50
51
-
We'd also need a style property that will transition the intermediary values here, in this case:
51
+
我们可能还需要一个样式属性来对中间值做过渡,在这个例子中:
52
52
53
53
```
54
54
.box {
@@ -59,7 +59,7 @@ We'd also need a style property that will transition the intermediary values her
59
59
<pdata-height="450"data-theme-id="5162"data-slug-hash="983220ed949ac670dff96bdcaf9d3338"data-default-tab="result"data-user="sdras"data-embed-version="2"data-pen-title="Custom Scroll Directive- CSS Transition"class="codepen">See the Pen <ahref="https://codepen.io/sdras/pen/983220ed949ac670dff96bdcaf9d3338/">Custom Scroll Directive- CSS Transition</a> by Sarah Drasner (<ahref="https://codepen.io/sdras">@sdras</a>) on <ahref="https://codepen.io">CodePen</a>.</p>
Though we would remove the previous CSS transition from this implementation because it's now handled with JavaScript.
94
+
而且我们会从实现中移除之前的 CSS 过渡,因为 JavaScript 已经搞定了这件事。
95
95
96
-
## The Benefit of Using Custom Directives
96
+
## 使用自定义指令的好处
97
97
98
-
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.
100
+
为元素附加或移除滚动事件是对于这项技术的一个很好的用例,因为就像其它我们使用的指令一样,它们有必要绑定在元素上,否则我们就需要寻找其 DOM 引用。这种模式避免了我们遍历 DOM 节点,保证事件的逻辑能够和需要引用的节点对应起来。
101
101
102
-
## Real-World Example: Using a Custom Scroll Directive for Cascading Animations
102
+
## 真实的示例:为级联动画使用一个自定义滚动指令
103
103
104
-
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.
106
+
为了保持代码的精炼和易读,我们会传递一些预设参数,诸如我们向下滚动页面时动画的起始点和终止点。
107
107
108
-
**This example is better viewed in the [full screen version](https://s.codepen.io/sdras/debug/078c19f5b3ed7f7d28584da450296cd0).**
<pdata-height="500"data-theme-id="5162"data-slug-hash="c8c55e3e0bba997350551dd747119100"data-default-tab="result"data-user="sdras"data-embed-version="2"data-pen-title="Scrolling Example- Using Custom Directives in Vue"class="codepen">See the Pen <ahref="https://codepen.io/sdras/pen/c8c55e3e0bba997350551dd747119100/">Scrolling Example- Using Custom Directives in Vue</a> by Sarah Drasner (<ahref="https://codepen.io/sdras">@sdras</a>) on <ahref="https://codepen.io">CodePen</a>.</p>
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`:
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.
@@ -147,8 +147,8 @@ We can then use this animation in our template, in this case we're attaching the
147
147
</clipPath>
148
148
```
149
149
150
-
## Alternative Patterns
150
+
## 替代模式
151
151
152
-
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).
0 commit comments