Skip to content

Commit dd3999d

Browse files
committed
update custom directives example to avoid force scroll on smaller screens
1 parent 52bc7a4 commit dd3999d

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/guide/custom-directive.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,43 @@ order: 16
66

77
## Intro
88

9-
In addition to the default set of directives shipped in core (`v-model` and `v-show`), Vue also allows you to register your own custom directives. Directives provide a reusable way of binding behavior that requires low-level DOM access, such as focusing on an input element.
9+
In addition to the default set of directives shipped in core (`v-model` and `v-show`), Vue also allows you to register your own custom directives. Directives provide a reusable way of binding behavior that requires low-level DOM access, such as focusing on an input element, like this one:
1010

11-
Here's a simple example:
11+
{% raw %}
12+
<div id="simplest-directive-example" class="demo">
13+
<input v-focus>
14+
</div>
15+
<script>
16+
Vue.directive('focus', {
17+
bind: function (el) {
18+
Vue.nextTick(function () {
19+
el.focus()
20+
})
21+
}
22+
})
23+
new Vue({
24+
el: '#simplest-directive-example'
25+
})
26+
</script>
27+
{% endraw %}
28+
29+
When the page loads, that element gains focus. In fact, if you haven't clicked on anything else since visiting this page, the input above should be focused now. Now let's build the directive that accomplishes this:
1230

1331
``` js
32+
// Register a global custom directive called v-focus
1433
Vue.directive('focus', {
34+
// When the directive is first bound to the element...
1535
bind: function (el) {
36+
// After the next render...
1637
Vue.nextTick(function () {
38+
// Focus the element
1739
el.focus()
1840
})
1941
}
2042
})
2143
```
2244

23-
This registers a global custom directive `v-focus`. If you want to register a directive locally instead, components also accept a `directives` option:
45+
If you want to register a directive locally instead, components also accept a `directives` option:
2446

2547
``` js
2648
directives: {
@@ -30,32 +52,12 @@ directives: {
3052
}
3153
```
3254

33-
Now in your template, you can use the `v-focus` attribute on any element, like this:
55+
Then in a template, you can use the new `v-focus` attribute on any element, like this:
3456

3557
``` html
3658
<input v-focus>
3759
```
3860

39-
When the page loads, that element will gain focus. In fact, if you haven't clicked on anything else since visiting this page, the input below should be focused now:
40-
41-
{% raw %}
42-
<div id="simplest-directive-example" class="demo">
43-
<input v-focus>
44-
</div>
45-
<script>
46-
Vue.directive('focus', {
47-
bind: function (el) {
48-
Vue.nextTick(function () {
49-
el.focus()
50-
})
51-
}
52-
})
53-
new Vue({
54-
el: '#simplest-directive-example'
55-
})
56-
</script>
57-
{% endraw %}
58-
5961
## Hook Functions
6062

6163
A directive definition object can provide several hook functions (all optional):

0 commit comments

Comments
 (0)