返回 slidev
animations.md
根目录 / docs / guide / animations.md
1 ---
2 outline: deep
3 ---
4
5 # Animation
6
7 Animation is an essential part of slide presentations. Slidev provides a variety of ways to animate your slides, from the simple to the complex. This guide will show you how to use them effectively.
8
9 ## Click Animation {#click-animation}
10
11 A "**click**" can be considered as the unit of animation steps in slides. A slide can have one or more clicks, and each click can trigger one or more animations - for example, revealing or hiding elements.
12
13 > [!NOTE]
14 > Since v0.48.0, we've rewritten the click animations system with much more consistent behaviors. It might change the behaviors of your existing slides in edge cases. While this page is showing the new click system, you can find more details about the refactor in [#1279](https://github.com/slidevjs/slidev/pull/1279).
15
16 ### `v-click` {#v-click}
17
18 To apply show/hide "click animations" for elements, you can use the `<v-click>` component or the `v-click` directive.
19
20 <!-- eslint-skip -->
21
22 ```md
23 <!-- Component usage:
24 this will be invisible until you press "next" -->
25 <v-click> Hello World! </v-click>
26
27 <!-- Directive usage:
28 this will be invisible until you press "next" the second time -->
29 <div v-click class="text-xl"> Hey! </div>
30 ```
31
32 ### `v-after` {#v-after}
33
34 `v-after` will turn the element visible when the previous `v-click` is triggered.
35
36 ```md
37 <div v-click> Hello </div>
38 <div v-after> World </div> <!-- or <v-after> World </v-after> -->
39 ```
40
41 When you press "next", both `Hello` and `World` will show up together.
42
43 ### Hide after clicking {#hide-after-clicking}
44
45 Add a `.hide` modifier to `v-click` or `v-after` directives to make elements invisible after clicking, instead of showing up.
46
47 ```md
48 <div v-click> Visible after 1 click </div>
49 <div v-click.hide> Hidden after 2 clicks </div>
50 <div v-after.hide> Hidden after 2 clicks </div>
51 ```
52
53 For the components, you can use the `hide` prop to achieve the same effect:
54
55 ```md
56 <v-click> Visible after 1 click </v-click>
57 <v-click hide> Hidden after 2 clicks </v-click>
58 <v-after hide> Also hidden after 2 clicks </v-after>
59 ```
60
61 ### `v-clicks` {#v-clicks}
62
63 `v-clicks` is only provided as a component. It's a shorthand to apply the `v-click` directive to all its child elements. It is especially useful when working with lists and tables.
64
65 ```md
66 <v-clicks>
67
68 - Item 1
69 - Item 2
70 - Item 3
71
72 </v-clicks>
73 ```
74
75 An item will become visible each time you click "next".
76 It accepts a `depth` prop for a nested list that is the only child:
77
78 ```md
79 <v-clicks depth="2">
80
81 - Item 1
82 - Item 1.1
83 - Item 1.2
84 - Item 2
85 - Item 2.1
86 - Item 2.2
87
88 </v-clicks>
89 ```
90
91 Paragraphs, lists, and tables are often mixed. Without an extra prop, `<v-clicks>` treats each list and each table as one block, so every item in that list appears together. `lists` and `tables` step into those blocks. The number is how many levels deep to reveal.
92
93 - `lists="1"` reveals each top-level item. Nested items stay with their parent.
94 - `lists="2"` also reveals nested items, one level further for each extra number.
95 - `tables="1"` reveals each body row. The header stays visible.
96 - `tables="2"` reveals each body cell.
97
98 ```md
99 <v-clicks lists="2" tables="1">
100
101 Here are the first priority items:
102
103 - Item 1
104 - Item 2
105 - Item 3
106
107 And others group of items:
108
109 - Medium:
110 - Sub item 1
111 - Sub item 2
112 - Minor:
113 - Sub item 3
114
115 | Name | Priority |
116 | ----- | -------- |
117 | Alpha | High |
118 | Beta | Low |
119
120 </v-clicks>
121 ```
122
123 Also, you can use the `every` prop to specify the number of items to show after each click:
124
125 ```md
126 <v-clicks every="2">
127
128 - Item 1.1
129 - Item 1.2
130 - Item 2.1
131 - Item 2.2
132
133 </v-clicks>
134 ```
135
136 ### Positioning {#positioning}
137
138 By default, the clicking animations are triggered one by one. You can customize the animation "position" of elements by using the `at` prop or the `v-click` directive with value.
139
140 Like the CSS layout system, click-animated elements can be "relative" or "absolute":
141
142 #### Relative Position {#relative-position}
143
144 This actual position of relative elements is calculated based on the previous relative elements:
145
146 ````md
147 <div v-click> visible after 1 click </div>
148 <v-click at="+2"><div> visible after 3 clicks </div></v-click>
149 <div v-click.hide="'-1'"> hidden after 2 clicks </div>
150
151 ```js {none|1|2}{at:'+5'}
152 1 // highlighted after 7 clicks
153 2 // highlighted after 8 clicks
154 ```
155 ````
156
157 > [!NOTE]
158 > The default value of `v-click` is `'+1'` when you don't specify it.
159
160 In fact, `v-after` are just shortcuts for `v-click` with `at` prop:
161
162 ```md
163 <!-- The following 2 usages are equivalent -->
164 <img v-after />
165 <img v-click="'+0'" />
166
167 <!-- The following 3 usages are equivalent -->
168 <img v-click />
169 <img v-click="'+1'" />
170 <v-click-gap size="1" /><img v-after />
171 ```
172
173 ::: tip `at` prop value format
174 Only string values starting with `'+'` or `'-'` like `'+1'` are treated as relative positions:
175
176 | Value | Kind |
177 | -------------- | -------- |
178 | `'-1'`, `'+1'` | Relative |
179 | `+1` === `1` | Absolute |
180 | `'1'` | Absolute |
181
182 So don't forget the single quotes for the relative values.
183 :::
184
185 #### Absolute Position {#absolute-position}
186
187 The given value is the exact click count to trigger this animation:
188
189 ````md
190 <div v-click="3"> visible after 3 clicks </div>
191 <v-click at="2"><div> visible after 2 clicks </div></v-click>
192 <div v-click.hide="1"> hidden after 1 click </div>
193
194 ```js {none|1|2}{at:3}
195 1 // highlighted after 3 clicks
196 2 // highlighted after 4 clicks
197 ```
198 ````
199
200 #### Mixed Case {#mixed-case}
201
202 You can mix the absolute and relative positions:
203
204 ```md
205 <div v-click> visible after 1 click </div>
206 <div v-click="3"> visible after 3 clicks </div>
207 <div v-click> visible after 2 click </div>
208 <div v-click="'-1'"> visible after 1 click </div>
209 <div v-click="4"> visible after 4 clicks </div>
210 ```
211
212 The following example synchronizes the highlighting of the two code blocks:
213
214 ````md {1,6}
215 ```js {1|2}{at:1}
216 1 + 1
217 'a' + 'b'
218 ```
219
220 ```js {1|2}{at:1}
221 = 2
222 = 'ab'
223 ```
224 ````
225
226 ### Enter & Leave {#enter-leave}
227
228 You can also specify the enter and leave index for the `v-click` directive by passing an array. The end index is exclusive.
229
230 ```md
231 <div v-click.hide="[2, 4]">
232 This will be hidden at click 2 and 3 (and shown otherwise).
233 </div>
234 <div v-click />
235 <div v-click="['+1', '+1']">
236 This will be shown only at click 2 (and hidden otherwise).
237 </div>
238 ```
239
240 You can also use `v-switch` to achieve the same effect:
241
242 ```md
243 <v-switch>
244 <template #1> show at click 1, hide at click 2. </template>
245 <template #2> show at click 2, hide at click 5. </template>
246 <template #5-7> show at click 5, hide at click 7. </template>
247 </v-switch>
248 ```
249
250 See [`VSwitch` Component](/builtin/components#vswitch) for more details.
251
252 ### Mount on Click {#mount-on-click}
253
254 By default, `v-click` only hides elements with CSS: they are mounted when the slide is rendered, and they keep their space in the layout. Add the `.mount` modifier to mount the element only when its click is reached, and unmount it when it's hidden again, like `v-if`:
255
256 ```md
257 <div v-click.mount> Mounted after 1 click </div>
258 <div v-after.mount> Mounted together with the previous one </div>
259 <div v-click.mount="3"> Mounted after 3 clicks </div>
260 <div v-click.mount="[2, 4]"> Mounted at click 2 and 3 only </div>
261 <div v-click.mount.hide="2"> Unmounted after 2 clicks </div>
262 <div v-click.mount.fade-in.up> Mounted with an animation preset </div>
263
264 <template v-click.mount="5">
265 <p> Mounted after 5 clicks </p>
266 <p> Together with this one </p>
267 </template>
268 ```
269
270 For the components, use the `mount` prop:
271
272 ```md
273 <v-click mount at="3"> Mounted after 3 clicks </v-click>
274 <v-click mount hide> Unmounted after the next click </v-click>
275 <v-click mount :at="[2, 4]"> Mounted at click 2 and 3 only </v-click>
276 <v-click mount animation="fade-in up"> Mounted with an animation preset </v-click>
277 <v-after mount> Mounted together with the previous click </v-after>
278 ```
279
280 Positions work the same way as without `.mount`, including relative values and ranges. [Animation presets](#click-animation-presets) work too: the element is mounted with the hidden styles (`opacity: 0`), and they are removed on the next frame, so it animates in like a normal `v-click`. When it's hidden again, the content is unmounted on the click, and its element is removed once it has transitioned back to the hidden styles. Hidden elements don't take up space, and lifecycle hooks such as `@vue:mounted` and `@vue:unmounted` run at the click:
281
282 ```md
283 <!-- `onMounted` of this component runs at click 3, not when the slide loads -->
284 <MyConfetti v-click.mount="3" />
285
286 <div v-click.mount="2" @vue:mounted="console.log('click 2 reached')" />
287 ```
288
289 > [!TIP]
290 >
291 > - Each root element of the content is animated on its own. Text is wrapped in a `<span>`. A component that renders several root nodes is still mounted and unmounted on the click, but it can't be animated.
292 > - Click animations inside mounted content are registered too late to be counted, so don't nest `v-click` inside a `.mount` element.
293 > - The slide can also be rendered in other places, such as the next-click preview in presenter mode and the overview, where the content is mounted for that preview too. Check [`$renderContext`](/guide/global-context#render-context) if your code should only run in the main slide.
294
295 ### Custom Total Clicks Count {#total}
296
297 By default, Slidev automatically calculates how many clicks are required before going to the next slide. You can override this via the `clicks` frontmatter option:
298
299 ```yaml
300 ---
301 # 10 clicks in this slide, before going to the next slide
302 clicks: 10
303 ---
304 ```
305
306 ### Click Animation Presets {#click-animation-presets}
307
308 You can define default click animation presets in headmatter/frontmatter:
309
310 ```yaml
311 ---
312 clickAnimation: up
313 ---
314 ```
315
316 Or specify them per element with directive modifiers:
317
318 ```md
319 <div v-click>Uses default animation preset defined in frontmatter</div>
320 <div v-click.scale>Scales up when showing and scales down when hiding</div>
321 <div v-click.fade.right>Composes multiple animations: fade and right</div>
322 <div v-click.none>No animation for this element</div>
323 ```
324
325 There are some built-in presets available for you to use:
326
327 - `fade`: Fade in from opacity 0.5 to 1
328 - `fade-in`: Fade in from opacity 0 to 1
329 - `up`: Translate 20px up
330 - `down`: Translate 20px down
331 - `left`: Translate 20px left
332 - `right`: Translate 20px right
333 - `scale`: Scale to 0.9
334 - `none`: Disables animation for this element
335
336 And you can also define your own presets with custom CSS rules for `.slidev-vclick-anim-{presetName}`:
337
338 ```css
339 .slidev-vclick-anim-pop.slidev-vclick-hidden {
340 transform: scale(0.96) translateY(8px);
341 }
342 ```
343
344 ### Element Transitions {#element-transitions}
345
346 When you apply the `v-click` directive to your elements, it will attach the class name `slidev-vclick-target` to it. When the elements are hidden, the class name `slidev-vclick-hidden` will also be attached. For example:
347
348 ```html
349 <div class="slidev-vclick-target slidev-vclick-hidden">Text</div>
350 ```
351
352 After a click, it may become:
353
354 ```html
355 <div class="slidev-vclick-target">Text</div>
356 ```
357
358 By default, a subtle opacity transition is applied to those classes:
359
360 ```css
361 /* below shows the default style */
362
363 .slidev-vclick-target {
364 transition: opacity 100ms ease;
365 }
366
367 .slidev-vclick-hidden {
368 opacity: 0;
369 pointer-events: none;
370 }
371 ```
372
373 You can override them to customize the transition effects in your custom stylesheets. For example, you can achieve the scaling up transitions by:
374
375 ```css
376 /* styles.css */
377
378 .slidev-vclick-target {
379 transition: all 500ms ease;
380 }
381
382 .slidev-vclick-hidden {
383 transform: scale(0);
384 }
385 ```
386
387 To specify animations for only certain slides or layouts:
388
389 ```scss
390 .slidev-page-7,
391 .slidev-layout.my-custom-layout {
392 .slidev-vclick-target {
393 transition: all 500ms ease;
394 }
395
396 .slidev-vclick-hidden {
397 transform: scale(0);
398 }
399 }
400 ```
401
402 Learn more about [customizing styles](/custom/directory-structure#style). To target whichever slide is currently visible instead of a fixed slide number, you can use `.slidev-page-active` (see [Targeting the Active Slide](/custom/directory-structure#targeting-the-active-slide)).
403
404 ## Motion {#motion}
405
406 Slidev has [@vueuse/motion](https://motion.vueuse.org/) built-in. You can use the `v-motion` directive to any elements to apply motion to them. For example
407
408 ```html
409 <div
410 v-motion
411 :initial="{ x: -80 }"
412 :enter="{ x: 0 }"
413 :leave="{ x: 80 }"
414 >
415 Slidev
416 </div>
417 ```
418
419 The text `Slidev` will move from `-80px` to its original position when entering the slide. When leaving, it will move to `80px`.
420
421 > Before v0.48.9, you need to add `preload: false` to the slide's frontmatter to enable motion.
422
423 ### Motion with Clicks {#motion-with-clicks}
424
425 > Available since v0.48.9
426
427 You can also trigger the motion by clicks:
428
429 ```html
430 <div
431 v-motion
432 :initial="{ x: -80 }"
433 :enter="{ x: 0, y: 0 }"
434 :click-1="{ x: 0, y: 30 }"
435 :click-2="{ y: 60 }"
436 :click-2-4="{ x: 40 }"
437 :leave="{ y: 0, x: 80 }"
438 >
439 Slidev
440 </div>
441 ```
442
443 Or combine `v-click` with `v-motion`:
444
445 ```html
446 <div v-click="[2, 4]" v-motion
447 :initial="{ x: -50 }"
448 :enter="{ x: 0 }"
449 :leave="{ x: 50 }"
450 >
451 Shown at click 2 and hidden at click 4.
452 </div>
453 ```
454
455 The meanings of variants:
456
457 - `initial`: When `currentPage < thisPage`, or `v-click` hides the current element because `$clicks` is too small.
458 - `enter`: When `currentPage === thisPage`, and `v-click` shows the element. _Priority: lowest_
459 - `click-x`: `x` is a number representing the **absolute** click num. The variant will take effect if `$clicks >= x`. _Priority: `x`_
460 - `click-x-y`: The variant will take effect if `x <= $clicks < y`. _Priority: `x`_
461 - `leave`: `currentPage > thisPage`, or `v-click` hides the current element because `$clicks` is too large.
462
463 The variants will be combined according to the priority defined above.
464
465 ::: warning
466 Due to a Vue internal [bug](https://github.com/vuejs/core/issues/10295), currently **only** `v-click` applied to the same element as `v-motion` can control the motion animation. As a workaround, you can use something like `v-if="3 < $clicks"` to achieve the same effect.
467 :::
468
469 Learn more: [Demo](https://sli.dev/demo/starter/10) | [@vueuse/motion](https://motion.vueuse.org/) | [v-motion](https://motion.vueuse.org/features/directive-usage) | [Presets](https://motion.vueuse.org/features/presets)
470
471 ## Slide Transitions {#slide-transitions}
472
473 <div id="pages-transitions" />
474
475 Slidev supports slide transitions out of the box. You can enable it by setting the `transition` frontmatter option:
476
477 ```md
478 ---
479 transition: slide-left
480 ---
481 ```
482
483 This will give you a nice sliding effects on slide switching. Setting it in the headmatter will apply this to all slides. You can also set different transitions per slide in frontmatters.
484
485 ### Builtin Transitions {#builtin-transitions}
486
487 - `fade` - Crossfade in/out
488 - `fade-out` - Fade out and then fade in
489 - `slide-left` - Slides to the left (slide to right when going backward)
490 - `slide-right` - Slides to the right (slide to left when going backward)
491 - `slide-up` - Slides to the top (slide to bottom when going backward)
492 - `slide-down` - Slides to the bottom (slide to top when going backward)
493 - `view-transition` - Via the view transitions API
494
495 ### View Transition API {#view-transitions}
496
497 The View Transitions API provides a mechanism for easily creating animated transitions between different DOM states. Learn more about it in [View Transitions API - MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API).
498
499 :::warning
500 Experimental: This is not supported by all browsers. Check the [Browser compatibility table](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API#browser_compatibility) carefully before using this.
501 :::
502
503 You can use the `view-transition-name` CSS property to name view transitions, which creates connections between different page elements and smooth transitions when switching slides.
504
505 You can enable [Comark Syntax](/guide/syntax#comark-syntax) support to conveniently name view-transitions:
506
507 ```md
508 ---
509 transition: view-transition
510 comark: true
511 ---
512
513 # View Transition {.inline-block.view-transition-title}
514
515 ---
516
517 # View Transition {.inline-block.view-transition-title}
518 ```
519
520 ### Custom Transitions {#custom-transitions}
521
522 Slidev's slide transitions are powered by [Vue Transition](https://vuejs.org/guide/built-ins/transition.html). You can provide your custom transitions by:
523
524 ```md
525 ---
526 transition: my-transition
527 ---
528 ```
529
530 and then in your custom stylesheets:
531
532 ```css
533 .my-transition-enter-active,
534 .my-transition-leave-active {
535 transition: opacity 0.5s ease;
536 }
537
538 .my-transition-enter-from,
539 .my-transition-leave-to {
540 opacity: 0;
541 }
542 ```
543
544 Learn more about how it works in [Vue Transition](https://vuejs.org/guide/built-ins/transition.html).
545
546 ### Forward & Backward Transitions {#forward-backward-transitions}
547
548 You can specify different transitions for forward and backward navigation using `|` as a separator in the transition name:
549
550 ```md
551 ---
552 transition: go-forward | go-backward
553 ---
554 ```
555
556 With this, when you go from slide 1 to slide 2, the `go-forward` transition will be applied. When you go from slide 2 to slide 1, the `go-backward` transition will be applied.
557
558 ### Advanced Usage {#advanced-usage}
559
560 The `transition` field accepts an option that will passed to the [`<TransitionGroup>`](https://vuejs.org/api/built-in-components.html#transition) component. For example:
561
562 ```md
563 ---
564 transition:
565 name: my-transition
566 enterFromClass: custom-enter-from
567 enterActiveClass: custom-enter-active
568 ---
569 ```
570
570 lines MARKDOWN