Skip to content

Animation ​

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.

Click Animation ​

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.

NOTE

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.

v-click ​

To apply show/hide "click animations" for elements, you can use the <v-click> component or the v-click directive.

md
<!-- Component usage:
     this will be invisible until you press "next" -->
<v-click> Hello World! </v-click>

<!-- Directive usage:
     this will be invisible until you press "next" the second time -->
<div v-click class="text-xl"> Hey! </div>

v-after ​

v-after will turn the element visible when the previous v-click is triggered.

md
<div v-click> Hello </div>
<div v-after> World </div>  <!-- or <v-after> World </v-after> -->

When you press "next", both Hello and World will show up together.

Hide after clicking ​

Add a .hide modifier to v-click or v-after directives to make elements invisible after clicking, instead of showing up.

md
<div v-click> Visible after 1 click </div>
<div v-click.hide> Hidden after 2 clicks </div>
<div v-after.hide> Hidden after 2 clicks </div>

For the components, you can use the hide prop to achieve the same effect:

md
<v-click> Visible after 1 click </v-click>
<v-click hide> Hidden after 2 clicks </v-click>
<v-after hide> Also hidden after 2 clicks </v-after>

v-clicks ​

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.

md
<v-clicks>

- Item 1
- Item 2
- Item 3

</v-clicks>

An item will become visible each time you click "next". It accepts a depth prop for a nested list that is the only child:

md
<v-clicks depth="2">

- Item 1
  - Item 1.1
  - Item 1.2
- Item 2
  - Item 2.1
  - Item 2.2

</v-clicks>

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.

  • lists="1" reveals each top-level item. Nested items stay with their parent.
  • lists="2" also reveals nested items, one level further for each extra number.
  • tables="1" reveals each body row. The header stays visible.
  • tables="2" reveals each body cell.
md
<v-clicks lists="2" tables="1">

Here are the first priority items:

- Item 1
- Item 2
- Item 3

And others group of items:

- Medium:
  - Sub item 1
  - Sub item 2
- Minor:
  - Sub item 3

| Name  | Priority |
| ----- | -------- |
| Alpha | High     |
| Beta  | Low      |

</v-clicks>

Also, you can use the every prop to specify the number of items to show after each click:

md
<v-clicks every="2">

- Item 1.1
- Item 1.2
- Item 2.1
- Item 2.2

</v-clicks>

Positioning ​

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.

Like the CSS layout system, click-animated elements can be "relative" or "absolute":

Relative Position ​

This actual position of relative elements is calculated based on the previous relative elements:

md
<div v-click> visible after 1 click </div>
<v-click at="+2"><div> visible after 3 clicks </div></v-click>
<div v-click.hide="'-1'"> hidden after 2 clicks </div>

```js {none|1|2}{at:'+5'}
1  // highlighted after 7 clicks
2  // highlighted after 8 clicks
```

NOTE

The default value of v-click is '+1' when you don't specify it.

In fact, v-after are just shortcuts for v-click with at prop:

md
<!-- The following 2 usages are equivalent -->
<img v-after />
<img v-click="'+0'" />

<!-- The following 3 usages are equivalent -->
<img v-click />
<img v-click="'+1'" />
<v-click-gap size="1" /><img v-after />

at prop value format

Only string values starting with '+' or '-' like '+1' are treated as relative positions:

ValueKind
'-1', '+1'Relative
+1 === 1Absolute
'1'Absolute

So don't forget the single quotes for the relative values.

Absolute Position ​

The given value is the exact click count to trigger this animation:

md
<div v-click="3"> visible after 3 clicks </div>
<v-click at="2"><div> visible after 2 clicks </div></v-click>
<div v-click.hide="1"> hidden after 1 click </div>

```js {none|1|2}{at:3}
1  // highlighted after 3 clicks
2  // highlighted after 4 clicks
```

Mixed Case ​

You can mix the absolute and relative positions:

md
<div v-click> visible after 1 click </div>
<div v-click="3"> visible after 3 clicks </div>
<div v-click> visible after 2 click </div>
<div v-click="'-1'"> visible after 1 click </div>
<div v-click="4"> visible after 4 clicks </div>

The following example synchronizes the highlighting of the two code blocks:

md
```js {1|2}{at:1}
1 + 1
'a' + 'b'
```

```js {1|2}{at:1}
= 2
= 'ab'
```

Enter & Leave ​

You can also specify the enter and leave index for the v-click directive by passing an array. The end index is exclusive.

md
<div v-click.hide="[2, 4]">
  This will be hidden at click 2 and 3 (and shown otherwise).
</div>
<div v-click />
<div v-click="['+1', '+1']">
  This will be shown only at click 2 (and hidden otherwise).
</div>

You can also use v-switch to achieve the same effect:

md
<v-switch>
  <template #1> show at click 1, hide at click 2. </template>
  <template #2> show at click 2, hide at click 5. </template>
  <template #5-7> show at click 5, hide at click 7. </template>
</v-switch>

See VSwitch Component for more details.

Mount on Click ​

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:

md
<div v-click.mount> Mounted after 1 click </div>
<div v-after.mount> Mounted together with the previous one </div>
<div v-click.mount="3"> Mounted after 3 clicks </div>
<div v-click.mount="[2, 4]"> Mounted at click 2 and 3 only </div>
<div v-click.mount.hide="2"> Unmounted after 2 clicks </div>
<div v-click.mount.fade-in.up> Mounted with an animation preset </div>

<template v-click.mount="5">
  <p> Mounted after 5 clicks </p>
  <p> Together with this one </p>
</template>

For the components, use the mount prop:

md
<v-click mount at="3"> Mounted after 3 clicks </v-click>
<v-click mount hide> Unmounted after the next click </v-click>
<v-click mount :at="[2, 4]"> Mounted at click 2 and 3 only </v-click>
<v-click mount animation="fade-in up"> Mounted with an animation preset </v-click>
<v-after mount> Mounted together with the previous click </v-after>

Positions work the same way as without .mount, including relative values and ranges. 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:

md
<!-- `onMounted` of this component runs at click 3, not when the slide loads -->
<MyConfetti v-click.mount="3" />

<div v-click.mount="2" @vue:mounted="console.log('click 2 reached')" />

TIP

  • 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.
  • Click animations inside mounted content are registered too late to be counted, so don't nest v-click inside a .mount element.
  • 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 if your code should only run in the main slide.

Custom Total Clicks Count ​

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:

yaml
---
# 10 clicks in this slide, before going to the next slide
clicks: 10
---

Click Animation Presets ​

You can define default click animation presets in headmatter/frontmatter:

yaml
---
clickAnimation: up
---

Or specify them per element with directive modifiers:

md
<div v-click>Uses default animation preset defined in frontmatter</div>
<div v-click.scale>Scales up when showing and scales down when hiding</div>
<div v-click.fade.right>Composes multiple animations: fade and right</div>
<div v-click.none>No animation for this element</div>

There are some built-in presets available for you to use:

  • fade: Fade in from opacity 0.5 to 1
  • fade-in: Fade in from opacity 0 to 1
  • up: Translate 20px up
  • down: Translate 20px down
  • left: Translate 20px left
  • right: Translate 20px right
  • scale: Scale to 0.9
  • none: Disables animation for this element

And you can also define your own presets with custom CSS rules for .slidev-vclick-anim-{presetName}:

css
.slidev-vclick-anim-pop.slidev-vclick-hidden {
  transform: scale(0.96) translateY(8px);
}

Element Transitions ​

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:

html
<div class="slidev-vclick-target slidev-vclick-hidden">Text</div>

After a click, it may become:

html
<div class="slidev-vclick-target">Text</div>

By default, a subtle opacity transition is applied to those classes:

css
/* below shows the default style */

.slidev-vclick-target {
  transition: opacity 100ms ease;
}

.slidev-vclick-hidden {
  opacity: 0;
  pointer-events: none;
}

You can override them to customize the transition effects in your custom stylesheets. For example, you can achieve the scaling up transitions by:

css
/* styles.css */

.slidev-vclick-target {
  transition: all 500ms ease;
}

.slidev-vclick-hidden {
  transform: scale(0);
}

To specify animations for only certain slides or layouts:

scss
.slidev-page-7,
.slidev-layout.my-custom-layout {
  .slidev-vclick-target {
    transition: all 500ms ease;
  }

  .slidev-vclick-hidden {
    transform: scale(0);
  }
}

Learn more about customizing styles. To target whichever slide is currently visible instead of a fixed slide number, you can use .slidev-page-active (see Targeting the Active Slide).

Motion ​

Slidev has @vueuse/motion built-in. You can use the v-motion directive to any elements to apply motion to them. For example

html
<div
  v-motion
  :initial="{ x: -80 }"
  :enter="{ x: 0 }"
  :leave="{ x: 80 }"
>
  Slidev
</div>

The text Slidev will move from -80px to its original position when entering the slide. When leaving, it will move to 80px.

Before v0.48.9, you need to add preload: false to the slide's frontmatter to enable motion.

Motion with Clicks ​

Available since v0.48.9

You can also trigger the motion by clicks:

html
<div
  v-motion
  :initial="{ x: -80 }"
  :enter="{ x: 0, y: 0 }"
  :click-1="{ x: 0, y: 30 }"
  :click-2="{ y: 60 }"
  :click-2-4="{ x: 40 }"
  :leave="{ y: 0, x: 80 }"
>
  Slidev
</div>

Or combine v-click with v-motion:

html
<div v-click="[2, 4]" v-motion
  :initial="{ x: -50 }"
  :enter="{ x: 0 }"
  :leave="{ x: 50 }"
>
  Shown at click 2 and hidden at click 4.
</div>

The meanings of variants:

  • initial: When currentPage < thisPage, or v-click hides the current element because $clicks is too small.
  • enter: When currentPage === thisPage, and v-click shows the element. Priority: lowest
  • click-x: x is a number representing the absolute click num. The variant will take effect if $clicks >= x. Priority: x
  • click-x-y: The variant will take effect if x <= $clicks < y. Priority: x
  • leave: currentPage > thisPage, or v-click hides the current element because $clicks is too large.

The variants will be combined according to the priority defined above.

WARNING

Due to a Vue internal bug, 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.

Learn more: Demo | @vueuse/motion | v-motion | Presets

Slide Transitions ​

Slidev supports slide transitions out of the box. You can enable it by setting the transition frontmatter option:

md
---
transition: slide-left
---

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.

Builtin Transitions ​

  • fade - Crossfade in/out
  • fade-out - Fade out and then fade in
  • slide-left - Slides to the left (slide to right when going backward)
  • slide-right - Slides to the right (slide to left when going backward)
  • slide-up - Slides to the top (slide to bottom when going backward)
  • slide-down - Slides to the bottom (slide to top when going backward)
  • view-transition - Via the view transitions API

View Transition API ​

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.

WARNING

Experimental: This is not supported by all browsers. Check the Browser compatibility table carefully before using this.

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.

You can enable Comark Syntax support to conveniently name view-transitions:

md
---
transition: view-transition
comark: true
---

# View Transition {.inline-block.view-transition-title}

---

# View Transition {.inline-block.view-transition-title}

Custom Transitions ​

Slidev's slide transitions are powered by Vue Transition. You can provide your custom transitions by:

md
---
transition: my-transition
---

and then in your custom stylesheets:

css
.my-transition-enter-active,
.my-transition-leave-active {
  transition: opacity 0.5s ease;
}

.my-transition-enter-from,
.my-transition-leave-to {
  opacity: 0;
}

Learn more about how it works in Vue Transition.

Forward & Backward Transitions ​

You can specify different transitions for forward and backward navigation using | as a separator in the transition name:

md
---
transition: go-forward | go-backward
---

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.

Advanced Usage ​

The transition field accepts an option that will passed to the <TransitionGroup> component. For example:

md
---
transition:
  name: my-transition
  enterFromClass: custom-enter-from
  enterActiveClass: custom-enter-active
---